Reputation: 421
I want to find a regex that catch all strings that are not inside name('stringName') pattern.
For example I have this text:
fdlfksj "hello1" dsffsf "hello2\"hi" name("Tod").name('tod') 'hello3'
I want my regex to catch the strings:
"hello1", "hello2\"hi", 'hello3'
(it should also should catch "hello2\"hi"
because I want to ignore " escaping).
I want also that my regex will ignore "Tod" because it's inside the pattern name("...") How should I do it?
Here is my regex that doens't work:
((?<!(name\())("[^"]*"|'[^']*'))
It doesn't work with ignore escaping: \"
and \'
and it's also not ignore name("Tod")
How can I fix it?
Upvotes: 1
Views: 48
Reputation: 163342
You could get out of the way what you don't want, and use a capture group for what you want to keep.
The matches that you want are in capture group 2.
name\((['"]).*?\1\)|('[^'\\]*(?:\\.[^'\\]*)*'|"[^"\\]*(?:\\.[^"\\]*)*")
Explanation
name\((['"]).*?\1\)
Match name and then from the opening parenthesis till closing parenthesis between the same type of quote|
Or(
Capture group 2
('[^'\\]*(?:\\.[^'\\]*)*'
match the single quoted value including matching escapes ones|
Or[^"\\]*(?:\\.[^"\\]*)*"
The same for the double quotes)
Close group 2Upvotes: 0
Reputation: 15482
You can use the following regex:
(?<!name\()(["'])[^\)]+?(?<!\\)\1
It will match anything other than parenthesis ([^\)]+?
):
(["'])
- a quote symbol(?<!\\)\1
- the same quote symbol, which is not preceeded by a slashIn order to avoid getting the values that come after name(
, there's a condition that checks that (?<!name\()
.
Check the demo here.
Upvotes: 1
Reputation: 5380
(["'])((?:\\\1)|[^\1]*?)\1
(
Capturing group
["']
Match "
(double) or '
(single) quote)
Close group(
Capturing group
(?:
Non-capturing group
\\\1
Match \
followed by the quote by which it was started)
Close non-capturing group|
OR[^\1]*?
Non-gready match anything except a quote by which it was started)
Close group\1
Match the close quoteSee the demo
Upvotes: 0