Regular expression to select anything between * …. and …. *
Question:
Answers:
Your RegEx is matching the wrong type of slash. \*
is parsed from start to end, so \
is interpreted as the escape sequence for
, and that leaves your *
character unescaped. To fix this, use /*
to start your RegEx, or use /*
with a raw string in Python. Like this:
import re
test1 = '''
/*
this is a comment
*/
Not a comment
/*
new comment
*/
'''
test2 = '''
/*
comment 1
comment 3
*/
Not a comment
Not a comment
*/
'''
# out1 = re.findall(r'/*.*?*/', test1)
out1 = re.findall(r'/*([sS]*?)*/', test1)
print(out1)
out2 = re.findall(r'/*([sS]*?)*/', test2)
print(out2)
Your RegEx is matching the wrong type of slash. \*
is parsed from start to end, so \
is interpreted as the escape sequence for , and that leaves your
*
character unescaped. To fix this, use /*
to start your RegEx, or use /*
with a raw string in Python. Like this:
import re
test1 = '''
/*
this is a comment
*/
Not a comment
/*
new comment
*/
'''
test2 = '''
/*
comment 1
comment 3
*/
Not a comment
Not a comment
*/
'''
# out1 = re.findall(r'/*.*?*/', test1)
out1 = re.findall(r'/*([sS]*?)*/', test1)
print(out1)
out2 = re.findall(r'/*([sS]*?)*/', test2)
print(out2)