tags:

views:

40

answers:

1

I am trying to construct a regex to find a string in ruby

str = "foo"

I want to be able to stop trying to find the string after it finds the closing quotation mark. I also want to keep the quotation marks so I can output the string I found as:

puts "the string is:" + str
=> the string is: "foo"

I am pretty new to using regular expressions.

+2  A: 

Here is a start:

/".*?"/

Explanation:

"    Match a literal double quote.
.*?  Match any characters, as few as possible (non-greedy)
"    Match a second literal double quote.

Rubular

Note that this won't work if the string contains escaped quotes or is quoted with single quotes.

Mark Byers
Rubular is awesome. Just got turned onto it and it saves a lot of time, especially if your new to regular expressions.
Scott Radcliff