简体   繁体   English

awk正则表达式不匹配时

[英]awk regexp when not match

I got some help before here to construct a regexp that would match any lines that started with ":" then anything, then a "(" and after that anything, but it could not end with a ")" 在此之前我得到了一些帮助来构建一个正则表达式,该正则表达式匹配任何以“:”开头的行然后是任何东西,然后是“(”之后的任何内容,但它不能以“)结束”

The regexp I used was this: 我使用的正则表达式如下:

/:.+ \(.[^)]*$/

It seemes to work reasonably well. 它看起来工作得相当好。 I had this input lines: 我有这个输入行:

:web_application_settings (
:isakmp.phase1_DH_groups (
:ike_p1 (
:ike_p1_dh_grp (ReferenceObject
:isakmpkeymanager (lalalal)

And the output was only: :ike_p1_dh_grp (ReferenceObject 输出只是:: ike_p1_dh_grp(ReferenceObject

But then I happened to add the line: 但后来我碰巧添加了这条线:

:isakmpkeymanager ()

This shouldnt match, but it does, which I think is strange. 这不应该匹配,但确实如此,我认为这很奇怪。 What did I miss in the regexp that allowed this line that ends with a ")" to still match, when I use Do not match: [^)] followed by start to allow it to not match and then end of line. 我在正则表达式中错过了什么,允许以“)”结尾的这一行仍然匹配,当我使用不匹配时:[^]]然后开始允许它不匹配然后结束行。

Try: 尝试:

awk '/:.+ \([^)]+$/' File

What went wrong 什么地方出了错

Let's look at the original regex, /:.+ \\(.[^)]*$/ , one step at a time: 让我们一步一步地查看原始正则表达式, /:.+ \\(.[^)]*$/

  1. : meaning a single colon :意思是单个冒号

  2. .+ meaning one or more of any character. .+表示任何一个或多个角色。 ( + means one or more .) +表示一个或多个 。)

  3. meaning one blank 意思是一片空白

  4. \\( meaning one open parens \\(意思是一个公开的parens

  5. . meaning one of any character 即任何字符之一

  6. [^)]* meaning zero or more of any character except close parens. [^)]*表示除了密切的parens之外的任何字符的零或更多 ( * means zero or more .) *表示零或更多 。)

  7. $ meaning the end of the line. $表示该行的结尾。

The problem was step 5 which can match a ) . 问题是步骤5,它可以匹配a ) In the revised regex, this was eliminated and [^)]* was replaced with [^)]+ . 在修订的正则表达式中,这被删除, [^)]*[^)]+替换。

The . . after the \\( allows any single arbitrary character - including ) - immediately after the opening parenthesis. 之后\\(允许任何单个任意字符 - 包括) - 紧接在左括号之后。 If that's not what you want to say, take it out. 如果那不是你想说的话,那就把它拿出去吧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM