1

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")

enter image description here

How can I fix it?

0

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

9 Comments

thanks! it's great but I want to ignore only name(...) not all values between parentheses, how can I do it?
Just remove that extra check. Updated the answer accordingly. @RoG
but your answer now catch strings inside name(...).
can you provide a regex101 link? @RoG
link I want it will catch the string "hh" inside tye(...)
|
0
(["'])((?:\\\1)|[^\1]*?)\1

Regex Explanation

  • ( 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 quote

See the demo

Comments

0

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

Regex demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.