Regexp to cut all text inside the external quotation signs

Viewed 66

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
3 Answers

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

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

You can solve this problem with nested regexp operators:

SELECT regexp_replace(Regexp_substr(regexp_replace(word,'(^")|("$)'),'["].+'),'(^")') as Result

from(

 SELECT  '"some text "have "some text" here "that should" be cut"' as word from dual)
Related