javascript - 正则表达式仅获取特定行
问题描述
我试图只提取一个特定的行,之后没有任何其他字符。例如:
permit ip any any
permit oped any any eq 10.52.5.15
permit top any any (sdfg)
permit sdo any host 10.51.86.17 eq sdg
我只想匹配第一行permit ip any any
而不是其他行。需要注意的是,第二个单词ip
可以是任何单词。
意思是,我只发现permit (anyword) any any
,如果第二个之后有一个字符,则不匹配。
我试图这样做,\bpermit.\w+.(?:any.any).([$&+,:;=?@#|'<>.^*()%!-\w].+)
但发现除了permit ip any any
. 我确实尝试进行反向查找,但没有成功。
解决方案
我试图这样做,
\bpermit.\w+.(?:any.any).([$&+,:;=?@#|'<>.^*()%!-\w].+)
但发现除了 permit ip any any 之外的其他行。我确实尝试进行反向查找,但没有成功。
让我们拆开你的正则表达式,看看你的正则表达式是什么:
\b # starting on a word boundary (space to non space or reverse)
permit # look for the literal characters "permit" in that order
. # followed by any character
\w+ # followed by word characters (letters, numbers, underscores)
. # followed by any character
(?: # followed by a non-capturing group that contains
any # the literal characters 'any'
. # any character
any # the literal characters 'any'
)
. # followed by any character <-- ERROR HERE!
( # followed by a capturing group
[$&+,:;=?@#|'<>.^*()%!-\w] # any one of these many characters or word characters
.+ # then any one character one or more times
)
你描述的行为...
但这会找到除 permit ip any any 之外的其他行。
匹配您指定的内容。具体来说,上面的正则表达式要求在“任何任何”之后有字符。因为permit \w+ any any
该部分后面没有任何字符,所以正则表达式在我上面的细分any any
中的标记处失败。<-- ERROR HERE!
如果必须捕获最后一部分(使用捕获组)但它可能不存在,则可以使用?
字符将整个最后一部分设为可选。
这看起来像:
permit \w+ any any(?: (.+))?
细分:
permit # the word permit
[ ] # a literal space
\w+ # one or more word characters
[ ] # a literal space
any # the word any
[ ] # another literal space
any # another any; all of this is requred.
(?: # a non-capturing group to start the "optional" part
[ ] # a literal space after the any
(.+) # everything else, including spaces, and capture it in a group
)? # end non-capturing group, but make it optional
推荐阅读
- php - 返回值为 0 的验证函数
- clndr.js - CLNDR.js selectedDate 在第一次点击是空
- html - 最小化网格 CSS 的麻烦
- angular - 如何获取Angular6组件的内部html并将其添加到组件模板中?
- github - Github页面:文件列表之前的自述文件,而不是之后?
- java - 无法创建新服务:selenium 独立服务器中的 ChromeDriverService 错误
- python - django 测试服务器错误:连接被拒绝。如何在本地部署应用程序进行测试?
- cplex - 如何在 OPL 代码中将我的目标更改为“最小化 Max() - Min()”?
- javascript - Not returning a component on button press in React-Native
- tensorflow - Tensorflow签入文件中的“变量”和“变量/动量”有什么区别?