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: Tapani Tarvainen
Subject: Re: Pattern matching lines starting with double comment characters
Date: Sat, 25 Feb 2023 15:44:15 +0200

On Sat, Feb 25, 2023 at 11:25:53AM +0000, goncholden via (help-bash@gnu.org) 
wrote:

> I want to match lines that start with double comment characters in
> source code files.

What are you matching them with? Different tools have different
regular expressions.

> I am currently using
> 
> pn_ere = "^[[:space:]]*([#;!]+|@c|//)[[:space:]]+"​

That looks like an extended regexp. Possibly you're using
grep -E, or bash =~ operator.

So that allows for one or more of #, ; and ! in the beginning of the
comment before the space(s), but exactly two slashes (//), or @c.

> The pattern handles possible spaces at the beginning and end of lines.
> 
> But the pattern does not specify double comment characters
> 
> Case of ##
> Case of ;;
> Case of !!
> 
> How can adapt the pattern for these cases?

So you want to allow two or more #, ; or ! but reject
lines with just one of them, if I understood correctly.

You can use {2,} instead of + to indicate "at least two".
E.g.,

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

Or equivalently, repeat the subpattern:

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

With egrep (grep -E) you could also do this:

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

If you want exactly two of #, ;; or !, just use {2} without the comma
in the first case or omit + after the subexpression in the latter two.
Then you could also fold the // case in like this:

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

-- 
Tapani Tarvainen



reply via email to

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