gpt4 book ai didi

python - 如何在 python 中使用正则表达式获取结束字符串?

转载 作者:太空宇宙 更新时间:2023-11-04 02:10:06 26 4
gpt4 key购买 nike

我有一些字符串,我在上一个问题中已经提到过

s = "[1,12:12] call basic_while1() Error Code: 1046. No database selected"

s1="[1,12:12] call add() Asdfjgg Error Code: 1046. No database aa selected"

s2= "[1,12:12] call add()"

s3= "[1,12:12]"

s4="Error Code: 1046. No database selected"

s5="END: Error Code: 2134. database doen't exist"

regex=(?:^\[(\d+),(\s?[0-9:]+)\](?:\s+(.+?)\s?)?(?:Error Code:.*)?$)|(?:()()()(Error Code:.*$))

使用那个正则表达式我得到类似的输出

['1', '12:12', 'call basic_while1()']
['1', '12:12', 'call add() Asdfjgg']
['1', '12:12', 'call add()']
['1', '12:12']
['', '', '', 'Error Code: 1046. No database selected']

现在我只想改变那个正则表达式并像这样输出

['', '', '','Error Code: 1046. No database selected']
['', '', '','Error Code: 1046. No database selected']
['', '', '']
['', '']
['Error Code: 1046. No database selected']

我修改为

(?:^\?:[(\d+),(\s?[0-9:]+)\](?:\s+(.+?)\s?$)?(Error Code:.*))|(?:()()()(Error Code:.*$))  

但是没用是否可以使用该正则表达式获得这样的输出?

最佳答案

试试

regex='Error Code: \d+.*'
match = re.search(regex, s)
if match:
print(match.group(0))
# Output:
# 'Error Code: 1046. No database selected'

关于python - 如何在 python 中使用正则表达式获取结束字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53880656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com