help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Pattern matching lines starting with double comment characters


From: Koichi Murase
Subject: Re: Pattern matching lines starting with double comment characters
Date: Sat, 25 Feb 2023 23:57:35 +0900

2023年2月25日(土) 22:44 Tapani Tarvainen <bash@tapanitarvainen.fi>:
> What are you matching them with? Different tools have different
> regular expressions.

The variable name provided in the original question implies ERE, and
ERE is unique (except one allows using implementation extensions).
Although grep's ERE (where newlines mean alternation) and awk's ERE
(where backslash escape is interpreted in bracket expressions) have
additional rules, these differences do not seem to be relevant for the
current question as far as I know.

> > But the pattern does not specify double comment characters
> >
> > Case of ##
> > Case of ;;
> > Case of !!
> >
> > How can adapt the pattern for these cases?

> [...]
> pn_ere = "^[[:space:]]*([#;!]{2,}|@c|//)[[:space:]]+"
> [...]
> pn_ere = "^[[:space:]]*([#;!][#;!]+|@c|//)[[:space:]]+"

The above two both match also #!, !#, #;, ;#, ;!, and !;, which do not
seem to be requested.

> With egrep (grep -E) you could also do this:
>
> pn_ere = "^[[:space:]]*(([#;!])\2+|@c|//)[[:space:]]+"

\2 is undefined in ERE. The back references are only defined in BRE.
It's an implementation extension and not always available with grep
-E.

Also, `egrep' is not standardized, and now GNU grep produces warning
messages when one tries to use `egrep'. `grep -E' should be always
used.

----

The only portable way is to specify each just the same as you do for //:

pn_ere = "^[[:space:]]*(##|;;|!!|@c|//)[[:space:]]+"



reply via email to

[Prev in Thread] Current Thread [Next in Thread]