首页 > 解决方案 > 正则表达式仅获取特定行

问题描述

我试图只提取一个特定的行,之后没有任何其他字符。例如:

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. 我确实尝试进行反向查找,但没有成功。

标签: javascriptregex

解决方案


我试图这样做,\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

推荐阅读