Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 仅获取特定行的正则表达式_Javascript_Regex - Fatal编程技术网

Javascript 仅获取特定行的正则表达式

Javascript 仅获取特定行的正则表达式,javascript,regex,Javascript,Regex,我试图只提取一个特定的行,没有任何其他字符后。例如: 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 我只想匹配第一行允许ip any any,而不匹配其他行。需要注意的是,第二个单词ip可以是任何单词 意思是,我发现只有允许(anyword)any any并且如果第二个any后面有字符,则不匹配 我试图执行\

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

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
我只想匹配第一行
允许ip any any
,而不匹配其他行。需要注意的是,第二个单词
ip
可以是任何单词

意思是,我发现只有
允许(anyword)any any
并且如果第二个any后面有字符,则不匹配


我试图执行
\bpermit.\w+。(?:any.any)。([$&+,:;=?@#|'。^*()%!-\w].+)
但找到了除
允许ip any any之外的其他行。我确实尝试进行反向查找,但没有成功。

在最后的“any”和
m
多行regexp标志之后使用
$
行尾定位

/^permit \w+ any any$/gm

如果您使用的是基于Java的正则表达式,那么可以在表达式中包含多行标志。JavaScript正则表达式不支持此语法

(?m)^permit \w+ any$
我试图执行
\bpermit.\w+。(?:any.any)。([$&+,:;=?@#|'。^*()%!-\w].+)
但找到了除许可证ip any 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    # 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

假设您正在使用PCRE,
^permit\w+any$
;如果您的regex方言不支持
\w
,请将您的问题告知您正在使用的regex工具或语言(根据我正在使用Javascript并在网站regexr.com上进行测试,我无法使用“^”和$,因为我正在执行此操作的程序不支持多行,因此它不是Javascript regex。这些锚定是javascript规范的必需部分。然后它确实支持
$
^
,您应该使用它们。感谢回复,但我只是在这个网站上测试表达式。我用来实现表达式的实际程序不支持多行。那是什么程序?我们不能在不知道的情况下回答你的问题。
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