简体   繁体   中英

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

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

  5. . meaning one of any character

  6. [^)]* meaning zero or more of any character except close parens. ( * means zero or more .)

  7. $ meaning the end of the line.

The problem was step 5 which can match 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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