Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > simple regex not working

Reply
Thread Tools

simple regex not working

 
 
John
Guest
Posts: n/a
 
      09-13-2008
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


 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      09-13-2008
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.
>
> 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
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
 
Reply With Quote
 
 
 
 
Dr.Ruud
Guest
Posts: n/a
 
      09-13-2008
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}

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      09-13-2008
"John" <(E-Mail Removed)> 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}


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

$error = "";
return;
}

--
John http://johnbokma.com/ - Hacking & Hiking in Mexico

Perl help in exchange for a gift:
http://johnbokma.com/perl/help-in-ex...or-a-gift.html
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      09-14-2008
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.

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
John Bokma
Guest
Posts: n/a
 
      09-14-2008
"Dr.Ruud" <(E-Mail Removed)> wrote:

> 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"...

--
John http://johnbokma.com/ - Hacking & Hiking in Mexico

Perl help in exchange for a gift:
http://johnbokma.com/perl/help-in-ex...or-a-gift.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple, simple debug not working Jacob N. Ruby 0 12-29-2010 09:04 PM
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
regex bug (comments within regex not as robust) kg.google@olympiakos.com Perl Misc 3 10-27-2005 07:21 PM
wifi not working on new hp, or not working after live update =?Utf-8?B?RHJhZ29ueA==?= Wireless Networking 1 10-01-2005 11:17 PM
Re: not so simple regex problem nobull@mail.com Perl 0 07-09-2003 05:10 PM



Advertisments