new for regular expression in Perl

2006-01-03 Thread chen li
Hi all,

Here is my problem:

my $string="chen schen";

I want to use regular expression to find the exact
match in the string. So when I want to match "chen" I
expect "chen" only.
But  use the following line I get both "chen" and
"schen" at the same time.
$string=~/chen/g;

How do I get what I expect?

Thanks,

Li

 



__ 
Yahoo! DSL – Something to write home about. 
Just $16.99/mo. or less. 
dsl.yahoo.com 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: new for regular expression in Perl

2006-01-03 Thread Wagner, David --- Senior Programmer Analyst --- WGO
chen li wrote:
> Hi all,
> 
> Here is my problem:
> 
> my $string="chen schen";
> 
> I want to use regular expression to find the exact
> match in the string. So when I want to match "chen" I
> expect "chen" only.
> But  use the following line I get both "chen" and
> "schen" at the same time.
> $string=~/chen/g;
print $1 if ( $string =~ /(chen)/ );
Wags ;)
> 
> How do I get what I expect?
> 
> Thanks,
> 
> Li
> 
> 
> 
> 
> 
> __
> Yahoo! DSL - Something to write home about.
> Just $16.99/mo. or less.
> dsl.yahoo.com



***
This message contains information that is confidential
and proprietary to FedEx Freight or its affiliates.
It is intended only for the recipient named and for
the express purpose(s) described therein.
Any other use is prohibited.
***


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: new for regular expression in Perl

2006-01-03 Thread Timothy Johnson

Do you mean that you only want the first match, or that you only want
the word "chen" when it's not preceeded or followed by other characters?
You can use \b (word boundary) to make sure you got the whole word.

$string =~ /\b(chen)\b/g;

-Original Message-
From: chen li [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, January 03, 2006 3:54 PM
To: beginners@perl.org
Subject: new for regular expression in Perl

Hi all,

Here is my problem:

my $string="chen schen";

I want to use regular expression to find the exact
match in the string. So when I want to match "chen" I
expect "chen" only.
But  use the following line I get both "chen" and
"schen" at the same time.
$string=~/chen/g;

How do I get what I expect?

Thanks,

Li

 



__ 
Yahoo! DSL - Something to write home about. 
Just $16.99/mo. or less. 
dsl.yahoo.com 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: new for regular expression in Perl

2006-01-03 Thread Chris Charley


- Original Message - 
From: "chen li" <[EMAIL PROTECTED]>




Hi all,

Here is my problem:

my $string="chen schen";

I want to use regular expression to find the exact
match in the string. So when I want to match "chen" I
expect "chen" only.
But  use the following line I get both "chen" and
"schen" at the same time.
$string=~/chen/g;

How do I get what I expect?


Hi Chen

You can get the results by adding a \b before and after your reg expression. 
\b is a boundary between a word and a non-word character. (A word character 
is a-z, A-Z, 0-9, or underscore, _).So, for your example, schen wouldn't 
match then because the 's' preceding 'c' is a word character and so the \b 
wouldn't be true. But, it would match chen because the (non) character 
(beginning of the string) preceding the 'c' would make \b true.


$string=~/\bchen\b/g;

MATCH
"chen "
"#chen"
"here is a chen and another chen"
"chen's"(the apostrophy is a non-word char)

NO MATCH
"schen"
"chens"

Chris 




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: new for regular expression in Perl

2006-01-04 Thread chen li
Thanks Chris and others for the information.

Chris, I have another question: I have a file
containing multiple lines and it looks like this:

(line 1).chen.
(line 2)..
(line 3) chen.

If I read the whole file at once  and change it into a
string I have no problem using regular expression to
find out the word "chen". But it looks like a little
bit unnatural for me because it changes the file's
format. Is it possible to do the match without change
the file format? One way I think is to use a loop to
read the file line by line and do the match for each
line. I wonder if  this is the best way to get the job
done.

Once again thank you very much,

Li


--- Chris Charley <[EMAIL PROTECTED]> wrote:

> 
> - Original Message - 
> From: "chen li" <[EMAIL PROTECTED]>
> 
> 
> > Hi all,
> >
> > Here is my problem:
> >
> > my $string="chen schen";
> >
> > I want to use regular expression to find the exact
> > match in the string. So when I want to match
> "chen" I
> > expect "chen" only.
> > But  use the following line I get both "chen" and
> > "schen" at the same time.
> > $string=~/chen/g;
> >
> > How do I get what I expect?
> 
> Hi Chen
> 
> You can get the results by adding a \b before and
> after your reg expression. 
> \b is a boundary between a word and a non-word
> character. (A word character 
> is a-z, A-Z, 0-9, or underscore, _).So, for your
> example, schen wouldn't 
> match then because the 's' preceding 'c' is a word
> character and so the \b 
> wouldn't be true. But, it would match chen because
> the (non) character 
> (beginning of the string) preceding the 'c' would
> make \b true.
> 
> $string=~/\bchen\b/g;
> 
> MATCH
> "chen "
> "#chen"
> "here is a chen and another chen"
> "chen's"(the apostrophy is a non-word char)
> 
> NO MATCH
> "schen"
> "chens"
> 
> Chris 
> 
> 
> 
> -- 
> To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> For additional commands, e-mail:
> [EMAIL PROTECTED]
> 
> 
> 
> 
> 




__ 
Yahoo! DSL – Something to write home about. 
Just $16.99/mo. or less. 
dsl.yahoo.com 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: new for regular expression in Perl

2006-01-04 Thread Shashidhara Bapat
Hi Chen,

You can do one line at a time also.

(Also, if you read whole file, convert it into a string and work on that
string, the original file will not change.)

- Regards,
  Shashi.


On 1/4/06, chen li <[EMAIL PROTECTED]> wrote:
>
> Thanks Chris and others for the information.
>
> Chris, I have another question: I have a file
> containing multiple lines and it looks like this:
>
> (line 1).chen.
> (line 2)..
> (line 3) chen.
>
> If I read the whole file at once  and change it into a
> string I have no problem using regular expression to
> find out the word "chen". But it looks like a little
> bit unnatural for me because it changes the file's
> format. Is it possible to do the match without change
> the file format? One way I think is to use a loop to
> read the file line by line and do the match for each
> line. I wonder if  this is the best way to get the job
> done.
>
> Once again thank you very much,
>
> Li
>
>
> --- Chris Charley <[EMAIL PROTECTED]> wrote:
>
> >
> > - Original Message -
> > From: "chen li" <[EMAIL PROTECTED]>
> >
> >
> > > Hi all,
> > >
> > > Here is my problem:
> > >
> > > my $string="chen schen";
> > >
> > > I want to use regular expression to find the exact
> > > match in the string. So when I want to match
> > "chen" I
> > > expect "chen" only.
> > > But  use the following line I get both "chen" and
> > > "schen" at the same time.
> > > $string=~/chen/g;
> > >
> > > How do I get what I expect?
> >
> > Hi Chen
> >
> > You can get the results by adding a \b before and
> > after your reg expression.
> > \b is a boundary between a word and a non-word
> > character. (A word character
> > is a-z, A-Z, 0-9, or underscore, _).So, for your
> > example, schen wouldn't
> > match then because the 's' preceding 'c' is a word
> > character and so the \b
> > wouldn't be true. But, it would match chen because
> > the (non) character
> > (beginning of the string) preceding the 'c' would
> > make \b true.
> >
> > $string=~/\bchen\b/g;
> >
> > MATCH
> > "chen "
> > "#chen"
> > "here is a chen and another chen"
> > "chen's"(the apostrophy is a non-word char)
> >
> > NO MATCH
> > "schen"
> > "chens"
> >
> > Chris
> >
> >
> >
> > --
> > To unsubscribe, e-mail:
> > [EMAIL PROTECTED]
> > For additional commands, e-mail:
> > [EMAIL PROTECTED]
> > 
> > 
> >
> >
> >
>
>
>
>
> __
> Yahoo! DSL – Something to write home about.
> Just $16.99/mo. or less.
> dsl.yahoo.com
>
>
> --
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>  
>
>
>


Re: new for regular expression in Perl

2006-01-04 Thread chen li
Hi Shashi,

Thanks for the reply.

Sorry I didn't make myself clear enough in the
previous email. If I read the whole file into an array
(@file) and then change it into a scalar($string) the
position of each word will change from the second
line. If I want to know the position of each match the
  return position will be different from those in the
original file. This is what I mean " a little bit
unnatural for me".(You are correct that the original
file never changes unless you write something to it).
I think it might be natural for me to read the file
line by line and get the return position looks like
these(just an example), similar to do the word search
in microsoft Word, which is what I really want:

match in line 1 and the end of matching position is 4

match in line 4 and the end of matching position is 24

..
  

--- Shashidhara Bapat <[EMAIL PROTECTED]>
wrote:

> Hi Chen,
> 
> You can do one line at a time also.
> 
> (Also, if you read whole file, convert it into a
> string and work on that
> string, the original file will not change.)
> 
> - Regards,
>   Shashi.
> 
> 
> On 1/4/06, chen li <[EMAIL PROTECTED]> wrote:
> >
> > Thanks Chris and others for the information.
> >
> > Chris, I have another question: I have a file
> > containing multiple lines and it looks like this:
> >
> > (line 1).chen.
> > (line 2)..
> > (line 3) chen.
> >
> > If I read the whole file at once  and change it
> into a
> > string I have no problem using regular expression
> to
> > find out the word "chen". But it looks like a
> little
> > bit unnatural for me because it changes the file's
> > format. Is it possible to do the match without
> change
> > the file format? One way I think is to use a loop
> to
> > read the file line by line and do the match for
> each
> > line. I wonder if  this is the best way to get the
> job
> > done.
> >
> > Once again thank you very much,
> >
> > Li
> >
> >
> > --- Chris Charley <[EMAIL PROTECTED]> wrote:
> >
> > >
> > > - Original Message -
> > > From: "chen li" <[EMAIL PROTECTED]>
> > >
> > >
> > > > Hi all,
> > > >
> > > > Here is my problem:
> > > >
> > > > my $string="chen schen";
> > > >
> > > > I want to use regular expression to find the
> exact
> > > > match in the string. So when I want to match
> > > "chen" I
> > > > expect "chen" only.
> > > > But  use the following line I get both "chen"
> and
> > > > "schen" at the same time.
> > > > $string=~/chen/g;
> > > >
> > > > How do I get what I expect?
> > >
> > > Hi Chen
> > >
> > > You can get the results by adding a \b before
> and
> > > after your reg expression.
> > > \b is a boundary between a word and a non-word
> > > character. (A word character
> > > is a-z, A-Z, 0-9, or underscore, _).So, for your
> > > example, schen wouldn't
> > > match then because the 's' preceding 'c' is a
> word
> > > character and so the \b
> > > wouldn't be true. But, it would match chen
> because
> > > the (non) character
> > > (beginning of the string) preceding the 'c'
> would
> > > make \b true.
> > >
> > > $string=~/\bchen\b/g;
> > >
> > > MATCH
> > > "chen "
> > > "#chen"
> > > "here is a chen and another chen"
> > > "chen's"(the apostrophy is a non-word char)
> > >
> > > NO MATCH
> > > "schen"
> > > "chens"
> > >
> > > Chris
> > >
> > >
> > >
> > > --
> > > To unsubscribe, e-mail:
> > > [EMAIL PROTECTED]
> > > For additional commands, e-mail:
> > > [EMAIL PROTECTED]
> > > 
> > > 
> > >
> > >
> > >
> >
> >
> >
> >
> > __
> > Yahoo! DSL – Something to write home about.
> > Just $16.99/mo. or less.
> > dsl.yahoo.com
> >
> >
> > --
> > To unsubscribe, e-mail:
> [EMAIL PROTECTED]
> > For additional commands, e-mail:
> [EMAIL PROTECTED]
> > 
> 
> >
> >
> >
> 




__ 
Yahoo! DSL – Something to write home about. 
Just $16.99/mo. or less. 
dsl.yahoo.com 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: new for regular expression in Perl

2006-01-04 Thread Dave Gray
On 1/4/06, chen li <[EMAIL PROTECTED]> wrote:
> I think it might be natural for me to read the file
> line by line and get the return position looks like
> these(just an example), similar to do the word search
> in microsoft Word, which is what I really want:
>
> match in line 1 and the end of matching position is 4
>
> match in line 4 and the end of matching position is 24

perldoc -f pos
http://perldoc.perl.org/functions/pos.html

#!/usr/bin/perl
while () {
/(?<=bleh)/g && print "line $. matches [".pos($_)."]\n";
print substr($_, pos($_)-4, 4),"\n";
}
__DATA__
slkdfjslkdfjdksfjdbleh
sdfkjdblehblkdfjsldkfj
sldfkjdklfdskfdblehasfkjdsklf
slkdfjdkblehsldkfjdkfj

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: new for regular expression in Perl

2006-01-04 Thread Chris Charley


- Original Message - 
From: "chen li" <[EMAIL PROTECTED]>


[snip]


I think it might be natural for me to read the file
line by line and get the return position looks like
these(just an example), similar to do the word search
in microsoft Word, which is what I really want:

match in line 1 and the end of matching position is 4

match in line 4 and the end of matching position is 24


I think this is what you want.

You may want to read:
   perldoc -f pos
   perldoc perlvar (look for @- and @+ variables, LAST_MATCH_START and 
LAST_MATCH_END, respectively)


Chris


#!/usr/bin/perl
use strict;
use warnings;

while () {
   chomp;
   while (/\bchen\b/g) {
   print "match in line $. and the end of matching position is 
",pos,"\n";

   }
}

__DATA__
this chen has a chen
x
tyre
brit chen is good

*** Output

C:\perlp>perl t5.pl
match in line 1 and the end of matching position is 9
match in line 1 and the end of matching position is 20
match in line 4 and the end of matching position is 9




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]