Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^5: regex for strings with escaped quotes

by haukex (Bishop)
on Feb 12, 2019 at 16:47 UTC ( #1229821=note: print w/replies, xml ) Need Help??


in reply to Re^4: regex for strings with escaped quotes
in thread regex for strings with escaped quotes

The regex is:

(?:(?|(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))

which contains no capturing groups, hence $1 isn't populated. In my first bit of code, in the regex I added an extra set of parentheses, /($RE{delimited}{-delim=>'"'})/, so there is a capturing group there. That's the equivalent of:

((?:(?|(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\"))))

Alternatively, you can take the first regex above and just change the non-capturing group (?:...) that surrounds the entire expression into a capturing group:

((?|(?:\")(?:[^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))

Or, since you said you wanted to capture the stuff between the quotes, change that non-capturing group into a capturing one:

(?:(?|(?:\")([^\\\"]*(?:\\.[^\\\"]*)*)(?:\")))

Plus, there's a bunch of simplifications that could be made to the first regex above anyway (really just removing unnecessary groups):

/ \" [^\\\"]* (?: \\. [^\\\"]* )* \" /x

and just add capturing groups to that as needed.

Replies are listed 'Best First'.
Re^6: regex for strings with escaped quotes
by morgon (Priest) on Feb 12, 2019 at 17:11 UTC
    Ahh - I did not pay attention that you introducded a new capturing group by puttting the regex into an extra pair of brackets.

    Now it all makes sense - many thanks.

    I've never used Regexp::Common before but it seems to contain useful stuff - I see a bright future for it in my Go-Programs :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1229821]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2020-07-06 17:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?