-1

I've got the following regex:

regex = /\$([a-zA-Z.]+)/

and the following query

query = "Show me the PE Ratio for $AAPL, $TSLA"

Now regex.match(query) should capture AAPL and TSLA, but instead I get the following:

#<MatchData "$AAPL" 1:"AAPL">

which is completely wrong. Anyone know why?

Note that this regex works fine on Rubular: http://rubular.com/r/j0maQHnVFF

chintanparikh
  • 1,632
  • 6
  • 22
  • 36

2 Answers2

3

In Ruby the .match method will only return the first capture. You need it to return all captured matches, like the /g flag in PCRE

You can use the scan method. The scan method will either give you an array of all the matches or, if you pass it a block, pass each match to the block.

Code

query.scan(/\$([a-zA-Z.]+)/) 
MattSizzle
  • 3,145
  • 1
  • 22
  • 42
0

Fixed it, needed to use .scan instead of .match

chintanparikh
  • 1,632
  • 6
  • 22
  • 36