Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 711 Vote(s) - 3.48 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to find last occurrence of a number in a string using Ruby?

#1
Using Ruby... given the following 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 occurrence 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.
Reply

#2
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][1] match for as many non-digits as possible (zero or more). `$` represents the end of the string.

In Ruby you can use the [gsub][2] 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][3].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#3
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
Reply

#4
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"
Reply

#5
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/)
Reply

#6
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
Reply

#7
irb(main):068:0> "blah_blah.do.dah[4543]junk_junk".match(/(.+\d)/)[1]
=> "blah_blah.do.dah[4543"
Reply

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

Here is my answer:

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

If not, try this:

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

Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through