Using regular expressions in ruby ​​to find a string in quotes

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

str = "foo"

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

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

I am new to using regular expressions.

+3
source share
1 answer

Here is the 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 will not work if the string contains escaped quotes or is quoted with single quotes.

+2

Source: https://habr.com/ru/post/1766560/


All Articles