Simon Perepelitsa
Simon Perepelitsa

Reputation: 20649

How to get the particular part of string matching regexp in Ruby?

I've got a string Unnecessary:12357927251data and I need to select all data after colon and numbers. I will do it using Regexp.

string.scan(/:\d+.+$/)

This will give me :12357927251data, but can I select only needed information .+ (data)?

Upvotes: 11

Views: 16918

Answers (5)

jhleath
jhleath

Reputation: 804

Try this: /(?<=\:)\d+.+$/

It changes the colon to a positive look-behind so that it does not appear in the output. Note that the colon alone is a metacharacter and so must be escaped with a backslash.

Upvotes: 1

snorkpete
snorkpete

Reputation: 14574

Assuming that you are trying to get the string 'data' from your string, then you can use:

string.match(/.*:\d*(.*)/)[1]

String#match returns a MatchData object. You can then index into that MatchData object to find the part of the string that you want.

(The first element of MatchData is the original string, the second element is the part of the string captured by the parentheses)

Upvotes: 1

Dogbert
Dogbert

Reputation: 222398

Using IRB

irb(main):004:0> "Unnecessary:12357927251data".scan(/:\d+(.+)$/)
=> [["data"]]

Upvotes: 0

bta
bta

Reputation: 45087

Use parenthesis in your regular expression and the result will be broken out into an array. For example:

x='Unnecessary:12357927251data'
x.scan(/(:\d+)(.+)$/)
=> [[":12357927251", "data"]]
x.scan(/:\d+(.+$)/).flatten
=> ["data"]

Upvotes: 3

mckeed
mckeed

Reputation: 9827

Anything in parentheses in a regexp will be captured as a group, which you can access in $1, $2, etc. or by using [] on a match object:

string.match(/:\d+(.+)$/)[1]

If you use scan with capturing groups, you will get an array of arrays of the groups:

"Unnecessary:123data\nUnnecessary:5791next".scan(/:\d+(.+)$/)
=> [["data"], ["next"]]

Upvotes: 21

Related Questions