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?
CodePudding user response:
You can use the following regex:
(?<!name\()(["'])[^\)] ?(?<!\\)\1
It will match anything other than parenthesis ([^\)] ?):
- preceeded by
(["'])- a quote symbol - followed by
(?<!\\)\1- the same quote symbol, which is not preceeded by a slash
In order to avoid getting the values that come after name(, there's a condition that checks that (?<!name\().
Check the demo here.
CodePudding user response:
(["'])((?:\\\1)|[^\1]*?)\1
Regex Explanation
(Capturing group["']Match"(double) or'(single) quote
)Close group(Capturing group(?:Non-capturing group\\\1Match\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\1Match the close quote
See the demo
CodePudding user response:
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 2

