Home > Blockchain >  Regexp to cut all text inside the external quotation signs
Regexp to cut all text inside the external quotation signs

Time:07-02

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

CodePudding user response:

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

CodePudding user response:

Assuming you want to remove all text up to the first quotes then retain everything till the last quote, you can try this.

Demo

[[:alpha:]][^"]*\"(?<name>.*)"
  • Related