Search code examples
regexregex-groupregexp-replace

Regexp to cut all text inside the external quotation signs


Please help me with adjusting regexp. I need to cut all text inside the external quotation signs. I have text:

some text "have "some text" here "that should" be cut"

My regexp:

some text "(?<name>[^"]*)"

Need to get

have "some text" here "that should" be cut

But I've got

have

Solution

  • If you want to supported the first level of nested double quotes you can use

    some text "(?<name>[^"]*(?:"[^"]*"[^"]*)*)"
    

    See the regex demo.

    Details:

    • [^"]* - zero or more chars other than double quotes
    • (?:"[^"]*"[^"]*)* - zero or more repetitions of
      • "[^"]*" - a substring between double quotes that contains no other double quotes
      • [^"]* - zero or more chars other than double quotes.

    If your regex flavor supports recursion:

    some text ("(?<name>(?:[^"]++|\g<1>)*)")
    

    See this regex demo. Here, ("(?<name>(?:[^"]++|\g<1>)*)") is a capturing group #1 that matches

    • " - a " char
    • (?<name>(?:[^"]++|\g<1>)*) - Group "name": zero or more sequences of
      • [^"]++ - one or more chars other than "
      • | - or
      • \g<1> - Group 1 pattern recursed
    • " - a " char