Reputation: 772
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.
Upvotes: 3
Views: 2769
Reputation: 838096
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.
Note that this won't work if the string contains escaped quotes or is quoted with single quotes.
Upvotes: 3