NexAddo
NexAddo

Reputation: 772

using regular expressions in ruby to find a string in quotations

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

Answers (1)

Mark Byers
Mark Byers

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.

Rubular

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

Upvotes: 3

Related Questions