For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > reg expression again









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author reg expression again
Chen Li

2006-10-22, 9:56 pm

Hi all,

I write a small script as follows:

use strict;
use warnings;

my $file_name='OT-q1.001';

if ($file_name=~/(OT)*.(\d+$)/){
print "find it\t $file_name";
}else {print "No math";}

The problem is that it also macth the following
string:

my $file_name='I:/Common/Notebooks/Trans10C.032';
(ot in the Notebook)

Any help will be appreciated.

Li


________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
kens

2006-10-22, 9:56 pm


Chen Li wrote:
> Hi all,
>
> I write a small script as follows:
>
> use strict;
> use warnings;
>
> my $file_name='OT-q1.001';
>
> if ($file_name=~/(OT)*.(\d+$)/){
> print "find it\t $file_name";
> }else {print "No math";}
>
> The problem is that it also macth the following
> string:
>
> my $file_name='I:/Common/Notebooks/Trans10C.032';
> (ot in the Notebook)


Of course, here's what your regular expression does:

(OT)* => match zero or more instances of 'OT'
.. => match any character
(\d+$) => followed by 1 or more digits at the end of the string

So, you are not requiring that your string contain "ot".

I'm guessing you wanted /(OT).*(\d+$)/ (reverse the *.)

HTH,
Ken

>
> Any help will be appreciated.
>
> Li
>


kens

2006-10-22, 9:56 pm


kens wrote:
> Chen Li wrote:
>
> Of course, here's what your regular expression does:
>
> (OT)* => match zero or more instances of 'OT'
> . => match any character
> (\d+$) => followed by 1 or more digits at the end of the string
>
> So, you are not requiring that your string contain "ot".
>
> I'm guessing you wanted /(OT).*(\d+$)/ (reverse the *.)


Also, since you are not capturing any text with the parentheses, you
do not need them:

/OT.*\d+$/

Ken[color=darkred]
>
> HTH,
> Ken
>

John W. Krahn

2006-10-22, 9:56 pm

chen li wrote:
> Hi all,


Hello,

> I write a small script as follows:
>
> use strict;
> use warnings;
>
> my $file_name='OT-q1.001';
>
> if ($file_name=~/(OT)*.(\d+$)/){
> print "find it\t $file_name";
> }else {print "No math";}
>
> The problem is that it also macth the following
> string:
>
> my $file_name='I:/Common/Notebooks/Trans10C.032';
> (ot in the Notebook)


'ot' and 'OT' are not the same string so it won't match.


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
Chen Li

2006-10-22, 9:56 pm



--- "John W. Krahn" <krahnj@telus.net> wrote:

> chen li wrote:
>
> Hello,
>
>
> 'ot' and 'OT' are not the same string so it won't
> match.
>
>
> John


Hi John,

This is what get from the screen:

C:\Perl\self\beginner>perl reg1.pl
find it I:/Common/Notebooks/Trans10C.032

Li

________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Robin Sheat

2006-10-22, 9:56 pm

Chen Li

2006-10-22, 9:56 pm



--- Robin Sheat <robin@kallisti.net.nz> wrote:

> On Monday 23 October 2006 12:37, chen li wrote:
> Maybe you mean:
> if ($file_name=~/^OT.*\.(\d+$)/){
>

Thank you and it is what I want.

Li

________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com