Bulki
Bulki

Reputation: 741

Regex between " but not beyond

I want to use a regex to capture everything between " (including the " itself) The problem is this:

Regex:

\\\"(.[^,][^\\\"]*)\\\"

Text:

"text", text2, "text"
    meeeh = "Y"
else
    meeeh2 = "N"

with this regex, the folling is selected:

"text"    "text"
         "Y"
else
    meeeh2 = " 

The problem seems to be that the regex doesn't stop when nothing is behind the " or when there is a newline.

Any ideas?

Upvotes: 0

Views: 64

Answers (2)

Kendall Frey
Kendall Frey

Reputation: 44374

When it reaches the first " in "Y", this is what the regex does:

  • \" matches "
  • . matches Y
  • [^,] matches "
  • [^\"]* matches else meeeh2 =
  • \" matches "

Essentially you're looking for "Any character, then anything that's not a comma, then anything but double quotes until the end" between the quotes. This means at least 2 characters, but Y is only 1.

If you mean anything but quotes between quotes, use \"([^"]*)\". If you mean anything but quotes and commas, \"([^",]*)\" should do.

Upvotes: 1

vks
vks

Reputation: 67988

.*?(\".*?\").*?

Try this.Please have a look at the demo.

http://regex101.com/r/cA4wE0/7

Upvotes: 3

Related Questions