[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to match the less-than symbol in regex?
From: |
Andreas Kusalananda Kähäri |
Subject: |
Re: How to match the less-than symbol in regex? |
Date: |
Wed, 18 Mar 2020 08:05:29 +0100 |
On Tue, Mar 17, 2020 at 09:35:42PM -0400, Daniel Mills wrote:
> 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]+)<$'
$ [[ '9<' =~ ^([0-9]+)'<'$ ]]; echo $?
0
>
> --
> 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.
--
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden
.