我想编写一个正则表达式来搜索Cisco路由器配置文件,以便使用多个用户名。
示例:传递
username account1 privilege 0 password [在此处停止并继续下一行]
示例:失败
username account1 privilege 0 password [在此处停止并继续下一行] 用户名[在此处停止]
我想出了以下内容: username account1 privilege 0 password。 \ n(。 \ n)*。* username
但似乎遇到了灾难性的回溯问题。 http://www.regular-expressions.info/catastrophic.html
答案 0 :(得分:1)
如果要检测某行包含
username account1 privilege 0 password
但是在同一行上不包含额外的username
,您可以使用否定的预测:
/username account1 privilege 0 password(?!.*?username)/g
您可以在行动中看到here。它只是找到你想要的文本,然后检查你不想要的文本(在这种情况下,另一个username
)以后不会出现在同一行上。