Python 如何使用正则表达式获取除括号外的分号

Python 如何使用正则表达式获取除括号外的分号,python,regex,Python,Regex,对于以下C源代码片段: 正则表达式;(?![^(]*\)工作,但不在第一段代码上。使用自定义: re.sub(模式、应答、字符串、计数=0、标志=0) … 如果repl是一个函数,则为模式的每个非重叠出现调用它 函数repl针对单个的每次出现都被调用和。由于re.sub未找到重叠序列,因此第一个左括号将触发完全匹配,直到最后一个右括号 重新导入 def repl(m): 内容=m.组(1) 如果内容中有“(”: 返回内容 返回“;\n” str1='for(j=0;j您需要为每个正则表达式匹配计

对于以下C源代码片段:

正则表达式
;(?![^(]*\)
工作,但不在第一段代码上。

使用自定义:

re.sub(模式、应答、字符串、计数=0、标志=0)


如果
repl
是一个函数,则为
模式的每个非重叠出现调用它

函数
repl
针对单个
的每次出现都被调用和。由于
re.sub
未找到重叠序列,因此第一个左括号将触发完全匹配,直到最后一个右括号

重新导入
def repl(m):
内容=m.组(1)
如果内容中有“(”:
返回内容
返回“;\n”

str1='for(j=0;j您需要为每个正则表达式匹配计算开括号和闭括号,如果开括号多于闭括号,则只插入换行符。这是在替换()中完成的,该替换()在正则表达式的每个匹配中调用。正则表达式搜索“(”and“)”仅用于计算,搜索“;”保留或插入换行符

import re

def replacement(matched_list):
    global bracket_count
    matched_char=matched_list.group(1)
    if "(" in matched_char:
        bracket_count += 1
        # don't replace, just return what was found
        return matched_char 
    elif ")" in matched_char:
        bracket_count -= 1
        # don't replace, just return what was found
        return matched_char 
    elif ";" in matched_char:
        # if we're inside brackets, insert \n
        if bracket_count == 0:
            return ';\n'
        # if not, leave it intact
        else:
            return ';'

# example 1
bracket_count=0
code="for (j=0; j<len; j++) a = (s) + (4); test = 5;"
new_code = re.sub('([();] ?)', replacement, code)
print(code)
print(new_code)

# example 2
bracket_count=0
code="for (j=0; j<(len); (j++)) a = (s) + (4); test = 5;"
new_code = re.sub('([();])', replacement, code)
print(code)
print(new_code)

# example 3
bracket_count=0
code="for (j=0; j<len; j++) test = 5; a = (s) + (4);"
new_code = re.sub('([();])', replacement, code)
print(code)
print(new_code)
重新导入
def更换(匹配_列表):
全局括号计数
匹配的字符=匹配的列表。组(1)
如果“(”在匹配字符中:
方括号\u计数+=1
#不要替换,只返回找到的内容
返回匹配字符
匹配字符中的“elif”):
括号\u计数-=1
#不要替换,只返回找到的内容
返回匹配字符
匹配字符中的elif“;”
#如果在括号内,请插入\n
如果括号_计数==0:
返回“;\n”
#如果没有,请保持原样
其他:
返回“;”
#例1
括号内的计数=0

code=“for(j=0;j
(?
;(?!!.\))
MonkeyZeus-谢谢,但这不起作用。我只想得到分号,不想使用bracket@aria不,那祝你好运,因为正则表达式不能计数,所以你无法跟踪括号是否闭合。我将从if内容中删除所有return语句,而只执行一个
return matched\u char
在替换的
结尾处
括号计数==0
例外,因为这确实会更改输入。@3limin4t0r:no,替换结尾处的“单”返回匹配字符“不会像“返回”;”那样删除;后面的空格我想在分号之后插入
\n
,除了使用python代码正则表达式模块的括号外。“问题并没有涉及删除空格。这些只是假设。
for (j=0; j<(len); (j++)) a = (s) + (4); test = 5;
str1 = 'for (j=0; j<len; j++) test = 5; a = (s) + (4);'
for (j=0; j<len; j++) test = 5; a = (s) + (4);
import re

def replacement(matched_list):
    global bracket_count
    matched_char=matched_list.group(1)
    if "(" in matched_char:
        bracket_count += 1
        # don't replace, just return what was found
        return matched_char 
    elif ")" in matched_char:
        bracket_count -= 1
        # don't replace, just return what was found
        return matched_char 
    elif ";" in matched_char:
        # if we're inside brackets, insert \n
        if bracket_count == 0:
            return ';\n'
        # if not, leave it intact
        else:
            return ';'

# example 1
bracket_count=0
code="for (j=0; j<len; j++) a = (s) + (4); test = 5;"
new_code = re.sub('([();] ?)', replacement, code)
print(code)
print(new_code)

# example 2
bracket_count=0
code="for (j=0; j<(len); (j++)) a = (s) + (4); test = 5;"
new_code = re.sub('([();])', replacement, code)
print(code)
print(new_code)

# example 3
bracket_count=0
code="for (j=0; j<len; j++) test = 5; a = (s) + (4);"
new_code = re.sub('([();])', replacement, code)
print(code)
print(new_code)
for (j=0; j<len; j++) a = (s) + (4); test = 5;
for (j=0; j<len; j++) a = (s) + (4);
test = 5;

for (j=0; j<(len); (j++)) a = (s) + (4); test = 5;
for (j=0; j<(len); (j++)) a = (s) + (4);
test = 5;