simple regex not working

J

John

Hi

I'm trying to match a string beginning a letter A to R followed by any
letter (from S to Z) or any digit.

if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}

Well, it does not work.

Any ideas?

Regards
John
 
J

John W. Krahn

John said:
I'm trying to match a string beginning a letter A to R followed by any
letter (from S to Z) or any digit.

if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}

Well, it does not work.

Any ideas?

You are not telling it to match only at the beginning of the string:

if ( $x =~ /^([A-Ra-r][S-Zs-z0-9]+)/ ) {



John
 
D

Dr.Ruud

John W. Krahn schreef:
John wrote:
I'm trying to match a string beginning a letter A to R followed by
any letter (from S to Z) or any digit.
if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}
Well, it does not work.

You are not telling it to match only at the beginning of the string:
if ( $x =~ /^([A-Ra-r][S-Zs-z0-9]+)/ ) {

Also the capturing and the quantifier are likely not wanted:

if ( $x =~ /^[A-Ra-r][S-Zs-z0-9]/ ) {$error=""; return}
 
J

John Bokma

John said:
I'm trying to match a string beginning a letter A to R followed by any
letter (from S to Z) or any digit.

if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}

if ($x =~ /^[A-R][S-Z\d]/i ) {

$error = "";
return;
}
 
D

Dr.Ruud

John Bokma schreef:
John:
I'm trying to match a string beginning a letter A to R
followed by any letter (from S to Z) or any digit.

if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}

if ($x =~ /^[A-R][S-Z\d]/i ) {

$error = "";
return;
}

Ignoring the intricacies of /i and \d is not best practice, especially
when it is so easy to avoid as in this case.

Some reasons: lc() and uc() are not (always) symmetric, \d is
(sometimes) a character set with 200+ different characters.

If you mean [0-9], then you should write [0-9], not \d.

There is a plan to make \d (at last) equivalent to only [0-9] (and \w to
only [0-9A-Za-z_], etc.) in Perl 5.12, but that will break old code, so
a new modifier like /U for non-Unicode semantics might be better.
 
J

John Bokma

Dr.Ruud said:
John Bokma schreef:
John:
I'm trying to match a string beginning a letter A to R
followed by any letter (from S to Z) or any digit.

if ($x =~ /([A-Ra-r][S-Zs-z0-9]+)/) {$error=""; return}

if ($x =~ /^[A-R][S-Z\d]/i ) {

$error = "";
return;
}

Ignoring the intricacies of /i and \d is not best practice, especially
when it is so easy to avoid as in this case.

Some reasons: lc() and uc() are not (always) symmetric, \d is
(sometimes) a character set with 200+ different characters.

If you mean [0-9], then you should write [0-9], not \d.

Depends on your input data, no? Also, what does the OP mean with "any
digit"...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top