jgiunta
jgiunta

Reputation: 721

Match text between quotes

i need to match only text into "".

I tried with this code, but dosen't works :(

text = ""This is an example text to be mathed""
text.scan(/^(")$(")/)

Thanks in advance.

Upvotes: 0

Views: 1687

Answers (2)

d11wtq
d11wtq

Reputation: 35298

A better pattern for matching strings is:

/"(\\.|[^"])*"/

This will consume backslash escapes, but will stop at the first terminating " (unless it is preceded by a backslash).

Upvotes: 0

Chris Bunch
Chris Bunch

Reputation: 89823

So your example doesn't work because your string is malformed. That is, ""boo"" isn't a Ruby string. You could use single quotes to make a string with double quotes in it and do the match, like so:

>> boo = '"Sample text to be matched"'
=> ""Sample text to be matched"\n"
>> boo.scan(/"(.*)"/)
=> [["Sample text to be matched"]]

Upvotes: 3

Related Questions