如果需要匹配默认python re module中的递归模式,
你可以像我一样处理我最近为之构建的递归注释
css预处理器。在
通常使用re只用于将文本拆分为标记,然后使用循环
使用嵌套级别变量查找所有语法。这是我的代码:
COMMENTsRe = re.compile( r"""
// |
\n |
/\* |
\*/
""", re.X )
def rm_comments( cut ):
nocomment = 0 # no inside comment
c = 1 # c-like comments, but nested
cpp = 2 # c++like comments
mode = nocomment
clevel = 0 # nesting level of c-like comments
matchesidx = []
# in pure RE we cannot find nestesd structuries
# so we are just finding all boundires and parse it here
matches = COMMENTsRe.finditer( str(cut) )
start = 0
for i in matches:
m = i.group()
if mode == cpp:
if m == "\n":
matchesidx.append( ( start, i.end()-1 ) ) # -1 because without \n
mode = nocomment
elif mode == c:
if m == "/*":
clevel += 1
if m == "*/":
clevel -= 1
if clevel == 0:
matchesidx.append( ( start, i.end() ) )
mode = nocomment
else:
if m == "//":
start = i.start()
mode = cpp
elif m == "/*":
start = i.start()
mode = c
clevel += 1
cut.rm_and_save( matchesidx )
如果需要匹配默认python re module中的递归模式, 你可以像我一样处理我最近为之构建的递归注释 css预处理器。在
通常使用re只用于将文本拆分为标记,然后使用循环 使用嵌套级别变量查找所有语法。这是我的代码:
相关问题 更多 >
编程相关推荐