Stop scan splitting my match

JayTarka

When I scan for zan I get the entire group in one entry, behavior I expect and want:

"ax zan".scan /(zan)/ #=> [["zan"]] 

How can I have the whole match returned, white space and all ? i.e

"ax       b".scan /.../ #=> [["ax       b"]]

When I scan for (ax)\s*(b), the match is split into two entries:

"ax b".scan /(ax)\s*(b)/ #=> [["ax", "b"]]

Update

How do I use the | operator without groups?

"sab   x".scan /sab|p\s*x/ #=> [["sab   x"]]
"sap   x".scan /sab|p\s*x/ #=> [["sap   x"]]
Jordan Running

As you've discovered, when you use a RegExp with groups, String#scan will return an array of arrays, the inner arrays each having one element for each capture. If that's not what you want, then you have to make your groups non-capturing by using the ?: flag, e.g. (?:foo|bar).

expr = /sa(?:b|p)\s*x/
"sab   x".scan(expr) #=> ["sab   x"]
"sap   x".scan(expr) #=> ["sap   x"]

P.S. The above works, but since only one character differs, in this case you should use a character class instead:

/sa[bp]\s*x/

P.P.S. You should only use scan if you're looking for multiple matches. If you just want one match, use String#slice, which has the handy String#[] alias. This will return the match as a string instead of an array:

expr = /sa(?:b|p)\s*x/
"sab   x"[expr] #=> "sab   x"
"sap   x"[expr] #=> "sap   x"

In case it's not clear, this works on variables, too, like any other method:

str = "sab   x"
str[/sa[bp]\s*x/] #=> "sab   x"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related