pythonregex在3个在线测试仪上成功测试,但没有在cod中工作

2024-10-16 17:15:56 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正在尝试从下面的多行字符串的开头提取IP和结尾的数字。我已经成功地测试过了pythex.org网站, pyregex.com网站以及regex101.com,但在执行脚本时它不起作用。regex语句的输出如下。在

多行字符串(从使用paramiko到SSH的Cisco路由器提取):

sh ip bgp summ | in 192.168.190.
192.168.190.3   4          100     166     169       17    0    0 02:27:11            3
192.168.190.4   4          100     169     171       17    0    0 02:26:33            4
R1#

我的regex如下所示(我还尝试以常规方式使用regex标志,即re.M):

^{pr2}$

当我运行以下代码时:

print(type(string1))
print(type(re3))
print(len(re3))
print(re3)

输出如下:

<class 'str'>
<class 'list'>
0
[]

我错过什么了吗?在


Tags: 字符串orgipcom网站type结尾数字
1条回答
网友
1楼 · 发布于 2024-10-16 17:15:56

字符串包含回车符(\r,CR)。在

# without CR
>>> re.search('(?m)a$', 'a\n')  # matches
<_sre.SRE_Match object; span=(0, 1), match='a'>

# with CR
>>> re.search('(?m)a$', 'a\r\n')  # does not match
>>> re.search('(?m)a\r$', 'a\r\n')
<_sre.SRE_Match object; span=(0, 2), match='a\r'>

调整正则表达式以匹配CR;在$之前添加\r?(使用\r?使CR可选)


^{pr2}$

相关问题 更多 >