Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Ruby reg exp matching question

Status
Not open for further replies.

nacrotek

Programmer
Jul 20, 2005
5
US
Is it possible to get a complete list of matches made from a regular expression?

Here is an example of what I'm trying to do:

sTestStr = "exec Trace_p @Comment = 'this is a test string will it work?"
reTestRE = Regexp.new("(this|work)")

mdMatch = reTestRE.match(sTestStr)
puts mdMatch.to_a()

The output is:
this
this

So it looks like its matching the string twice, but I can't get access to the 'work' match. Can this be done?
 
Change [tt]puts mdMatch.to_a()[/tt] to [tt]puts mdMatch.captures[/tt]. However, you only have 1 capture.

Try this:
[tt]sTestStr.scan(reTestRE){|s| puts s}[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top