user3447041
user3447041

Reputation: 19

Python - Regular expressions, removing content between two delimiters

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

Answers (2)

You can try this:

output = re.sub("/\*[^\*]+\*/", '', input)

Otherwise you can try this one:

/\*[^\*]+\*/\s?

to get rid of the additional space.

Upvotes: 0

Wiktor Stribiżew
Wiktor Stribiżew

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

Related Questions