Reputation: 19
Problem; I am trying to remove the string inbetween and including /* */ for every occurrence inside the input string.
input = /* comment */ variable owner; /* comment */
output = variable owner;
Currently I have tried doing:
output = re.sub("//*[^]+*//", '', input)
output = re.sub("/*[^]+*//", '', input)
Any guidance would be appreciated.
Upvotes: 1
Views: 950
Reputation: 236
You can try this:
output = re.sub("/\*[^\*]+\*/", '', input)
Otherwise you can try this one:
/\*[^\*]+\*/\s?
to get rid of the additional space.
Upvotes: 0
Reputation: 626728
You cannot use [^]
pattern in Python, it is a JavaScript only pattern to match any character (as (?s).
in Python).
However, you may use a better multiline comment matching regex:
/\*[^*]*\*+(?:[^/*][^*]*\*+)*/
See this regex demo. It is an unrolled equivalent of (?s)/\*.*?\*/
matching /*
, then any 0+ chars up to the first */
.
import re
s = '/* comment */ variable owner; /* comment */'
rx = r'/\*[^*]*\*+(?:[^/*][^*]*\*+)*/'
print(re.sub(rx, '', s))
See the Python demo
Upvotes: 1