-4

Given text with some phrases between /* */, I would like to remove the strings/phrases that are inside.

For example:

aaaabbbbb /*ppppqqqqrrrsss sstttuuu*/cccccddddeee

desired output:

aaaabbbbb /**/cccccddddeee

I tried to use re.sub(r'(\/*{2,3}[\s\n]*)(?:.*?[\s\n]*)*([\n\s]*\/*{2,3}) from python's REGEX, but the * symbol isn't recognized.

Does anyone know where I am going wrong?

lbragile
  • 7,549
  • 3
  • 27
  • 64
Simone
  • 19
  • 5

1 Answers1

1

(?<=\/\*)[\s\S]*?(?=\*\/)

This regex uses a positive lookbehind to assert the presence of a preceding /* and a positive lookahead to assert the presence of a trailing */, and between the two captures any characters across any number of lines.

As pointed out in the comments, * is a special regex character, and needs to be escaped with a backslash, like \*.

Demo


The python code is as follows:

import re

text = "aaaabbbbb /*ppppqqqqrrrsss\n\nss/tt*tu/uu*/cccccddddeee"

print(re.sub('(?<=\/\*)[\s\S]*?(?=\*\/)', '', text))

Demo

Nick Reed
  • 4,989
  • 4
  • 17
  • 37