在同一行中匹配字符串的值,但在不同的lin中不匹配

2024-09-16 20:19:38 发布

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

从下面的输出中,我想得到端口描述的值

out = """Port Description - not advertised
         System Name - not advertised"""

截至目前,我正在使用以下代码:

re.search(r'Port Description\s+-\s+([\s\w]+)',out).group(1)

但是我得到的结果是:not advertised System Name

我的输出中只需要not advertised


Tags: 端口代码nameresearchportgroupnot
1条回答
网友
1楼 · 发布于 2024-09-16 20:19:38

您应该在多行模式(flags=re.M)下搜索,并包括行尾标记'$'

re.search(r'Port Description\s+-\s+([\s\w]+$)', out, flags=re.M).group(1)
#'not advertised'

相关问题 更多 >