Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cut string with ruby

Tags:

string

regex

ruby

I have strings:

до 100(прошло до 15 лет)
до 100 (прошло от 15 лет)
до 75
до 100 (10 лет)
до 100 (10 лет)

I want to cut strings like

до 100
до 100
до 75
до 100
до 100
like image 321
Vyacheslav Loginov Avatar asked Jun 27 '26 06:06

Vyacheslav Loginov


1 Answers

strings = ['до 100(прошло до 15 лет)',
           'до 100 (прошло от 15 лет)',
           'до 75',
           'до 100 (10 лет)',
           'до 100 (10 лет)']

strings.map! { |str| str[/до \d+/] }

p strings    #=> ["до 100", "до 100", "до 75", "до 100", "до 100"]
like image 200
Victor Deryagin Avatar answered Jun 28 '26 20:06

Victor Deryagin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!