Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: A Regular Expression Problem in Perl 5.28

5 views
Skip to first unread message

Uri Guttman

unread,
Mar 28, 2023, 4:45:06 PM3/28/23
to begi...@perl.org
On 3/28/23 16:17, Martin McCormick wrote:
>
> The string I am interested in testing for starts with 5
> or 6 digits in a row and all I need to do is determine that the
> first 5 or 6 characters are numbers Period. That's all.
>
>
> my $regextest = '/^\d+\{5,\}/' ;

why are you escaping the {}?? those are meta chars that are needed to
make that a 5+ range. just delete the backslashes on them and it will work.

you need to know when to backslash and when not too. all regex chars
which do something are not backslashed. alpha chars with backslashes are
regex char types and special chars.

thanx,

uri


--
https://uriguttman.blogspot.com/
A Long Strange Trip
A blog about computers, food, my life and silliness.

Uri Guttman

unread,
Mar 28, 2023, 5:15:06 PM3/28/23
to Martin McCormick, begi...@perl.org
On 3/28/23 17:01, Martin McCormick wrote:
> Uri Guttman <u...@perlhunter.com> writes:
>> why are you escaping the {}?? those are meta chars that are needed to make
>> that a 5+ range. just delete the backslashes on them and it will work.
> First, thank you but read on, please.
> I couldn't agree more. That should do it but when I
> don't escape them, I get:
>
> Nested quantifiers in regex; marked by <-- HERE in m//^\d+{ <-- HERE 3,}// at ./regex line 10.
>
you also quoted the whole regex in '' but included the // which are the
normal regex delimiters. remove the outer quotes.

and use the qr// form for regexes.

and you don't want the + after the \d as the {5,} is the count. you
can't have both types of repeat counts.

my $re = qr/^\d{5,}/ ;

that should be all you need.

read perlretut to learn more regex basics. you have escaping and quoting
mistakes in the original.

Uri Guttman

unread,
Mar 28, 2023, 5:45:06 PM3/28/23
to begi...@perl.org
On 3/28/23 17:14, Sam wrote:
> On 3/28/23 16:07, Uri Guttman wrote:
>>
>> my $re = qr/^\d{5,}/ ;
>>
>> that should be all you need.
>>
>> read perlretut to learn more regex basics. you have escaping and
>> quoting mistakes in the original.
>>
>> uri
>>
>
> I would think /^\d{5,6}/ would be what is needed? He wanted 5 or 6
> digits.
>

yes, but he kept the {5,} repeat count. so i just kept it too.

Sam

unread,
Mar 28, 2023, 5:45:06 PM3/28/23
to begi...@perl.org
On 3/28/23 16:07, Uri Guttman wrote:
> On 3/28/23 17:01, Martin McCormick wrote:
>> Uri Guttman <u...@perlhunter.com> writes:
>>> why are you escaping the {}?? those are meta chars that are needed to
>>> make
>>> that a 5+ range. just delete the backslashes on them and it will work.
>>     First, thank you but read on, please.
>>     I couldn't agree more.  That should do it but when I
>> don't escape them, I get:
>>
>> Nested quantifiers in regex; marked by <-- HERE in m//^\d+{ <-- HERE
>> 3,}// at ./regex line 10.
>>
> you also quoted the whole regex in '' but included the // which are the
> normal regex delimiters. remove the outer quotes.
>
> and use the qr// form for regexes.
>
> and you don't want the + after the \d as the {5,} is the count. you
> can't have both types of repeat counts.
>
> my $re = qr/^\d{5,}/ ;
>
> that should be all you need.
>
> read perlretut to learn more regex basics. you have escaping and quoting
> mistakes in the original.
>
> uri
>

I would think /^\d{5,6}/ would be what is needed? He wanted 5 or 6 digits.

--Sam

Uri Guttman

unread,
Mar 28, 2023, 7:00:05 PM3/28/23
to Martin McCormick, begi...@perl.org
On 3/28/23 18:00, Martin McCormick wrote:
> Uri Guttman <u...@perlhunter.com> writes:
>> yes, but he kept the {5,} repeat count. so i just kept it too.
> Now that I know how this works, I will probably change to
> {4,} as this would match 4 or more digits. From reading the
> documentation, {4} means 4 and only 4. {4,6} means 4 but nothing
> else except 6. {N,} means N as a low limit but any number higher.

you got most of it. but {4,6} means any length from 4 to 6. 5 is fine
there.

think of it as a low and high pair of lengths. the left one is the
shortest count and the right side is the longest.

if you have only {4}, that is the same as {4,4}.

{5,} is 5 or more (high count is infinity, sort of).

{,5} is {0,5} or any number of repeats up to 5.

+ is just {1,} 1 or more
* is just {0,} 0 or more
? is just {0,1} 0 or 1 - makes that part optional

and don't forget that any quantifier can modify any thing before it.
that is more than just single chars. if you group something, you can
apply a quantifier to it.

uri
0 new messages