views:

387

answers:

7

Using Ruby... Given a String x = "blah_blah.do.dah[4543]junk_junk"

How do I remove all text after the last number/digit?

I thought the easiest way to do this might be by finding the index of last occurence and then removing everything after that index. However, I can't seem to figure out how to obtain that index. All my attempts at using regex have failed.

Any helps is greately appreciated!

+2  A: 

This may be the regex you're looking for:

s/\D*$//

This regex matches all non-digits at the end of the string, starting from the last digit or the start of the string (if it doesn't contain any digits at all), and removes whatever is matched. More precisely, \D* is a greedy match for as many non-digits as possible (zero or more). $ represents the end of the string.

In Ruby you can use the gsub method of a string to search and replace using a regular expression:

x = 'blah_blah.do.dah[4543]junk_junk'
y = x.gsub(/\D*$/, '')

For more info on regexes and Ruby, see regular-expressions.info.

Stephan202
You probably just want /\D+$/ (or /\D+\Z/) -- the OP wanted to strip off non-digits from the end.
glenn jackman
@Glenn: doh, you're right... I'll update the answer.
Stephan202
A: 

I assume you want to include the last ']' after the number

Here is my answer:

Original_Text = "blah_blah.do.dah[4543]junk_junk"
Result___Text = Original_Text.gsub(Regexp.new("(.*\\[[0-9]+\\])[^0-9]*$"), '\1')
puts Result___Text

If not, try this:

Original_Text = "blah_blah.do.dah[4543]junk_junk"
Result___Text = Original_Text.gsub(Regexp.new("(.*[0-9]+)[^0-9]*$"), '\1')
puts Result___Text
NawaMan
Please don't use caps.
Justice
Caps? What do you mean?
NawaMan
variable names with a LeadingCapital are considered constants in Ruby.
glenn jackman
+1  A: 

This regex should work:

(.*(?=\d)\d)[^\d]*$

You should use something like:

result = your_text.gsub(/(.*(?=\d)\d)[^\d]*$/, '\\1')

Explanation:

  • (.*(?=\d)\d) is the group of thing you want to save
    • .* the . is everything except a line break, the * means 0 or more times
    • (?=\d) means until you are able to match a \d which is a digit
    • \d means match that digit also!
  • [^\d]+$ is the part you don't want to save
    • in [^\d]* matches anything that doesn't match \d and the * is again 0 or more times
    • the $ is the end of the line

An alternative could be simply replacing [^\d]+$ with nothing

Andrea Ambu
A: 
irb(main):068:0> "blah_blah.do.dah[4543]junk_junk".match(/(.+\d)/)[1]
=> "blah_blah.do.dah[4543"
pierr
+2  A: 

There are answers how to do what you need

Also to find last occurence of number:

x = 'blah_blah.do.dah[4543]junk_junk'
x.rindex(/\d/)
tig
Thanks. For some reason that didn't work I tried it... I must have has my syntax slightly incorrrect.Combining this with gsub worked beautifully.
Chadwick
A: 
puts "blah_blah.do.dah[4543]junk_junk"[/.*\d+/]
#returns "blah_blah.do.dah[4543"

using rindex:

x = "blah_blah.do.dah[4543]junk_junk"
x.rindex(/\d/)
puts "#{$`}#{$&}"
#also returns "blah_blah.do.dah[4543"
testr
A: 

All in all this seemed to be the cleanest & simplest way to do what I needed. Thank you all for pointing me in the right direction!

index = file.rindex(/\d/)
if(index) then
    p file[0 , index+1]
end
Chadwick