Remove characters between 1) opening and closing pattern and 2) if no closing pattern, opening pattern and end

You can use

gsub("\\*{3}.*?(?:\\*{3}|$)", "",  foostring)

See the R online demo

The TRE regex means:

  • \*{3} – three asterisks
  • .*? – any zero or more chars as few as possible
  • (?:\\*{3}|$) – a non-capturing group matching
    • \*{3} – either three asterisks
    • | – or
    • $ – end of string.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top