正则表达式 - 在特定模式下匹配 word/letters
Regex - Match word/letters underneath a specific pattern
非常不寻常,但我正在尝试匹配 SSH 会话的输出,该会话可能会折叠视图并低于所需的输出(如折叠的列)...
查看示例输出:
System Id Interface Circuit Id State HoldTime Type PRI
--------------------------------------------------------------------------------
rtr1.lab01.some GE0/0/1 0000000001 Up 22s L2 --
thing
rtr2.lab01.some GE0/0/2 0000000002 Up 24s L2 --
thingelse
我可以匹配第一行:
^([a-zA-Z0-9][a-zA-Z0-9.-]+[a-zA-Z0-9])
which returns(rtr1.lab01.some 和 rtr2.lab01.some)但我正在尝试根据完整的主机名 (rtr1.lab01.something 和 rtr2.lab01.somethingelse)
我也很好地匹配了其余的输出并且能够提取数据但真的找不到实现我正在尝试的方法...有人可以指出我正确的方向吗?进一步扩展(更多上下文...我在 Python 中使用 Google TextFSM 来匹配来自 SSH 会话的所有这些数据)
import re
text = """System Id Interface Circuit Id State HoldTime Type PRI
--------------------------------------------------------------------------------
rtr1.lab01.some GE0/0/1 0000000001 Up 22s L2 --
thing
rtr2.lab01.some GE0/0/2 0000000002 Up 24s L2 --
thingelse
rtr2.lab01.abcd GE0/0/4 0000000003 Up 24s L2 --
rtr2.lab01.none GE0/0/24 0000000004 Up 24s L2 --
sense
rtr2.lab01.efgh GE0/0/5 0000000003 Up 24s L2 --
"""
lines = text.rstrip().split('\n')[2:]
n_lines = len(lines)
current_line = -1
def get_next_line():
# Actual implementation would be reading from a file and yielding lines one line at a time
global n_lines, current_line, lines
current_line += 1
# return special sentinel if "end of file"
return lines[current_line] if current_line < n_lines else '$'
def get_system_id():
line = None
while line != '$': # loop until end of file
if line is None: # no current line
line = get_next_line()
if line == '$': # end of file
return
m = re.search(r'^([a-zA-Z0-9][a-zA-Z0-9.-]+[a-zA-Z0-9])', line)
id = m[1]
line = get_next_line() # might be sentinel
if line != '$' and re.match(r'^[a-zA-Z0-9]+$', line): # next line just single id?
id += line
line = None # will need new line
yield id
for id in get_system_id():
print(id)
打印:
rtr1.lab01.something
rtr2.lab01.somethingelse
rtr2.lab01.abcd
rtr2.lab01.nonesense
rtr2.lab01.efgh
非常不寻常,但我正在尝试匹配 SSH 会话的输出,该会话可能会折叠视图并低于所需的输出(如折叠的列)...
查看示例输出:
System Id Interface Circuit Id State HoldTime Type PRI
--------------------------------------------------------------------------------
rtr1.lab01.some GE0/0/1 0000000001 Up 22s L2 --
thing
rtr2.lab01.some GE0/0/2 0000000002 Up 24s L2 --
thingelse
我可以匹配第一行:
^([a-zA-Z0-9][a-zA-Z0-9.-]+[a-zA-Z0-9])
which returns(rtr1.lab01.some 和 rtr2.lab01.some)但我正在尝试根据完整的主机名 (rtr1.lab01.something 和 rtr2.lab01.somethingelse)
我也很好地匹配了其余的输出并且能够提取数据但真的找不到实现我正在尝试的方法...有人可以指出我正确的方向吗?进一步扩展(更多上下文...我在 Python 中使用 Google TextFSM 来匹配来自 SSH 会话的所有这些数据)
import re
text = """System Id Interface Circuit Id State HoldTime Type PRI
--------------------------------------------------------------------------------
rtr1.lab01.some GE0/0/1 0000000001 Up 22s L2 --
thing
rtr2.lab01.some GE0/0/2 0000000002 Up 24s L2 --
thingelse
rtr2.lab01.abcd GE0/0/4 0000000003 Up 24s L2 --
rtr2.lab01.none GE0/0/24 0000000004 Up 24s L2 --
sense
rtr2.lab01.efgh GE0/0/5 0000000003 Up 24s L2 --
"""
lines = text.rstrip().split('\n')[2:]
n_lines = len(lines)
current_line = -1
def get_next_line():
# Actual implementation would be reading from a file and yielding lines one line at a time
global n_lines, current_line, lines
current_line += 1
# return special sentinel if "end of file"
return lines[current_line] if current_line < n_lines else '$'
def get_system_id():
line = None
while line != '$': # loop until end of file
if line is None: # no current line
line = get_next_line()
if line == '$': # end of file
return
m = re.search(r'^([a-zA-Z0-9][a-zA-Z0-9.-]+[a-zA-Z0-9])', line)
id = m[1]
line = get_next_line() # might be sentinel
if line != '$' and re.match(r'^[a-zA-Z0-9]+$', line): # next line just single id?
id += line
line = None # will need new line
yield id
for id in get_system_id():
print(id)
打印:
rtr1.lab01.something
rtr2.lab01.somethingelse
rtr2.lab01.abcd
rtr2.lab01.nonesense
rtr2.lab01.efgh