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: Daniel Mills
Subject: Re: How to match the less-than symbol in regex?
Date: Tue, 17 Mar 2020 21:35:42 -0400

On 3/17/20, Daniel Mills <address@hidden> wrote:
> On Tue, Mar 17, 2020 at 8:27 PM Peng Yu <address@hidden> 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
>>
>> --
>> Regards,
>> Peng
>>
>>
> Quoted things on the right side are treated as literals. To get around
this
> issue,
> put the regex in a variable, and use an unquoted expansion.
>
> re='^([0-9]+)<$'; [[ '0<' =~ $re ]]; echo $?

So the above is the only way to get around this issue as the following
would give an error?

$ [[ '9<' =~ ^([0-9]+)<$ ]]
-bash: syntax error in conditional expression: unexpected token `<'
-bash: syntax error near `^([0-9]+)<$'

-- 
Regards,
Peng

You could also just escape/quote the < and leave the rest unquoted, as in
[[ '9<' =~ ^([0-9]+)\<$ ]]

But it's good practice to always do the regex-in-a-variable thing, it
avoids any issues like this.


reply via email to

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