Home > Archive > PERL Beginners > January 2006 > new for regular expression in Perl
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 |
new for regular expression in Perl
|
|
| Chen Li 2006-01-10, 4:02 am |
| 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
| |
| Wagner, David --- Senior Programmer Analyst --- WG 2006-01-10, 4:02 am |
| chen li wrote:
> Hi all,
>=20
> Here is my problem:
>=20
> my $string=3D"chen schen";
>=20
> 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=3D~/chen/g;
print $1 if ( $string =3D~ /(chen)/ );
Wags ;)
>=20
> How do I get what I expect?
>=20
> Thanks,
>=20
> Li
>=20
>=20
>=20
>=20
>=20
> ________________________________________
__
> 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.
****************************************
***************
| |
| Timothy Johnson 2006-01-10, 4:02 am |
|
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 =3D~ /\b(chen)\b/g;
-----Original Message-----
From: chen li [mailto:chen_li3@yahoo.com]=20
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=3D"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=3D~/chen/g;
How do I get what I expect?
Thanks,
Li
=20
=09
________________________________________
__=20
Yahoo! DSL - Something to write home about.=20
Just $16.99/mo. or less.=20
dsl.yahoo.com=20
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>
| |
| Chris Charley 2006-01-10, 4:02 am |
|
----- Original Message -----
From: "chen li" <chen_li3@yahoo.com>
> 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
| |
| Chen Li 2006-01-10, 4:02 am |
| 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 <charley@pulsenet.com> wrote:
>
> ----- Original Message -----
> From: "chen li" <chen_li3@yahoo.com>
>
>
> "chen" I
>
> 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:
> beginners-unsubscribe@perl.org
> For additional commands, e-mail:
> beginners-help@perl.org
> <http://learn.perl.org/>
> <http://learn.perl.org/first-response>
>
>
>
________________________________________
__
Yahoo! DSL – Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com
| |
| Chen Li 2006-01-10, 4:02 am |
| 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 <shashidharasbapat@gmail.com>
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 <chen_li3@yahoo.com> wrote:
> into a
> to
> little
> change
> to
> each
> job
> exact
> and
> and
> word
> because
> would
> beginners-unsubscribe@perl.org
> beginners-help@perl.org
> <http://learn.perl.org/first-response>
>
________________________________________
__
Yahoo! DSL – Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com
| |
| Dave Gray 2006-01-10, 4:02 am |
| On 1/4/06, chen li <chen_li3@yahoo.com> 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 (<DATA> ) {
/(?<=3Dbleh)/g && print "line $. matches [".pos($_)."]\n";
print substr($_, pos($_)-4, 4),"\n";
}
__DATA__
slkdfjslkdfjdksfjdbleh
sdfkjdblehblkdfjsldkfj
sldfkjdklfdskfdblehasfkjdsklf
slkdfjdkblehsldkfjdkfj
| |
| Chris Charley 2006-01-10, 4:02 am |
|
----- Original Message -----
From: "chen li" <chen_li3@yahoo.com>
[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 (<DATA> ) {
chomp;
while (/\bchen\b/g) {
print "match in line $. and the end of matching position is
",pos,"\n";
}
}
__DATA__
this chen has a chen
xxxxx
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
|
|
|
|
|