139

I'm currently trying to rexp a string into multiple variables. Example string:

ryan_string = "RyanOnRails: This is a test"

I've matched it with this regexp, with 3 groups:

ryan_group = ryan_string.scan(/(^.*)(:)(.*)/i)

Now to access each group I have to do something like this:

ryan_group[0][0] (first group) RyanOnRails
ryan_group[0][1] (second group) :
ryan_group[0][2] (third group) This is a test

This seems pretty ridiculous and it feels like I'm doing something wrong. I would be expect to be able to do something like this:

g1, g2, g3 = ryan_string.scan(/(^.*)(:)(.*)/i)

Is this possible? Or is there a better way than how I'm doing it?

ryanjones
  • 5,383
  • 4
  • 28
  • 24

5 Answers5

228

You don't want scan for this, as it makes little sense. You can use String#match which will return a MatchData object, you can then call #captures to return an Array of captures. Something like this:

#!/usr/bin/env ruby

string = "RyanOnRails: This is a test"
one, two, three = string.match(/(^.*)(:)(.*)/i).captures

p one   #=> "RyanOnRails"
p two   #=> ":"
p three #=> " This is a test"

Be aware that if no match is found, String#match will return nil, so something like this might work better:

if match = string.match(/(^.*)(:)(.*)/i)
  one, two, three = match.captures
end

Although scan does make little sense for this. It does still do the job, you just need to flatten the returned Array first. one, two, three = string.scan(/(^.*)(:)(.*)/i).flatten

Lee Jarvis
  • 16,031
  • 4
  • 38
  • 40
  • 7
    Beware that if no matches are found, match returns nil and you get a NilError. If you are in Rails, I suggest you to change: `one, two, three = string.match(/(^.*)(:)(.*)/i).captures` into: `one, two, three = string.match(/(^.*)(:)(.*)/i).try(:captures)` – Andrea Salicetti Jan 25 '13 at 10:17
  • 5
    @AndreaSalicetti I've edited my post, I'm not adding Rails-specific code to it so I have altered it with a version for handling the returned nil object – Lee Jarvis Jan 25 '13 at 18:26
  • 7
    You could also the new `&.` operator to get it back on a line and even using it twice when there is only one capture group. Eg.., `string.match(regex)&.captures&.first` – Gerry Shaw Mar 12 '17 at 22:35
49

You could use Match or =~ instead which would give you a single match and you could either access the match data the same way or just use the special match variables $1, $2, $3

Something like:

if ryan_string =~ /(^.*)(:)(.*)/i
   first = $1
   third = $3
end
Rado
  • 8,634
  • 7
  • 31
  • 44
35

You can name your captured matches

string = "RyanOnRails: This is a test"
/(?<one>^.*)(?<two>:)(?<three>.*)/i =~ string
puts one, two, three

It doesn't work if you reverse the order of string and the regex.

toonsend
  • 1,296
  • 13
  • 16
7

You have to decide whether it is a good idea, but ruby regexp can (automagically) define local variables for you!

I am not yet sure whether this feature is awesome or just totally crazy, but your regex can define local variables.

ryan_string = "RyanOnRails: This is a test"
/^(?<webframework>.*)(?<colon>:)(?<rest>)/ =~ ryan_string
# This defined three variables for you. Crazy, but true.
webframework # => "RyanOnRails"
puts "W: #{webframework} , C: #{colon}, R: #{rest}"

(Take a look at http://ruby-doc.org/core-2.1.1/Regexp.html , search for "local variable").

Note: As pointed out in a comment, I see that there is a similar and earlier answer to this question by @toonsend (https://stackoverflow.com/a/21412455). I do not think I was "stealing", but if you want to be fair with praises and honor the first answer, feel free :) I hope no animals were harmed.

Felix
  • 4,510
  • 2
  • 31
  • 46
  • This answer looks remarkably similar to http://stackoverflow.com/a/21412455/525478, which is over a year older... – Brad Werth Jan 27 '16 at 23:30
  • @BradWerth I guess I just did not see that. But I updated my answer to include your concerns. – Felix Jan 28 '16 at 14:38
6

scan() will find all non-overlapping matches of the regex in your string, so instead of returning an array of your groups like you seem to be expecting, it is returning an array of arrays.

You are probably better off using match(), and then getting the array of captures using MatchData#captures:

g1, g2, g3 = ryan_string.match(/(^.*)(:)(.*)/i).captures

However you could also do this with scan() if you wanted to:

g1, g2, g3 = ryan_string.scan(/(^.*)(:)(.*)/i)[0]
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306