help-bash
[Top][All Lists]
Advanced

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

Re: How to match the less-than symbol in regex?


From: Eli Schwartz
Subject: Re: How to match the less-than symbol in regex?
Date: Tue, 17 Mar 2020 20:37:59 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0

On 3/17/20 8:23 PM, Peng Yu wrote:
> Hi,
> 
> I am not able to match "<" in the regex. I am not sure what is wrong.
> Could anybody show me how to match "<" in regex? Thanks.
> 
> $ [[ '9<' =~ '^([0-9]+)<$' ]]; echo $?
> 1

This has NOTHING to do with the < character.

From man bash:

----------------------------------------------------------------------

An additional binary operator, =~, is available, with the same
precedence as == and !=. When it is used, the string to the right of the
 operator is considered a POSIX extended regular expression and matched
accordingly (as in regex(3)). The return value is 0 if the string
matches the pattern, and 1 otherwise. If the regular expression is
syntactically incorrect, the conditional expression's return value is 2.
If the nocasematch shell option is enabled, the match is performed
without regard to the case of alphabetic characters. Any part of the
pattern may be quoted to force the quoted portion to be matched as a
string. Bracket expressions in regular expressions must be treated
carefully, since normal quoting characters lose their meanings between
brackets. If the pattern is stored in a shell variable, quoting the
variable expansion forces the entire pattern to be matched as a string.

----------------------------------------------------------------------

You have quoted the right-hand side of the regex, this means it is no
longer a regex.

-- 
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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