Re: regex

[email protected] (Jorge Almeida) Mon, 22 Jan 2024 13:48:57 +0000
Newsgroups perl.beginners
Message-ID <CAKpSnpJyWdjUO2UzbOK1ZgN20hVFh3wwQV_52As-H1yO3rtbWw@mail.gmail.com>
On Mon, 22 Jan 2024 at 13:00, <[email protected]> wrote:

> Jorge Almeida:
> > Please help me to understand this:
> > $ perl -e 'exit(10) if "aaa"=~/a{,2}/;'
> > $ echo $?
> > $ 10
>
> In man perlre, under "Regular Expressions" it says:
>
>   {,n}        Match at most n times
>

Yes, I read it (several times). I still don't understand it (I understand
what you're saying, and I trust you're right, I just don't understand how
this behaviour matches the description above--- "at most", really?)

>
> So /a{,2}/ matches "", "a", and "aa" and is ignorant about what
> comes before and after (basically). That "aa" is followed by a
> "a" isn't something the expression prohibits. If you want that
>

Now, in
perl -e 'print $1,"\n" if "aaa"=~/(a{,2})/;'
$ aa
this is understandable. More or less. Maybe the semantics of /a{,2}/ should
be described as "match any number of consecutive 'a' whatsoever and capture
at most 2  'a' characters...

Thanks

Jorge Almeida


>