Home > Archive > PERL Beginners > October 2006 > Regexp Basics
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]
|
|
| Michael Alipio 2006-10-02, 3:58 am |
| Hi,
Suppose I have this:
#!/usr/local/bin/perl
use strict;
my $string = "Theres more than 1 way to do it";
if ($string =~ /\w+$/){
print "Hooray! pattern found";
print $1;
}
My goal is to print the last word.
However, it only prints "Hooray! pattern found";
Any idea what's wrong with $1??
Thanks
________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
| |
| Lee Goddard 2006-10-02, 3:58 am |
| > #!/usr/local/bin/perl
> use strict;
>=20
> my $string =3D "Theres more than 1 way to do it"; if ($string=20
> =3D~ /\w+$/){ print "Hooray! pattern found"; print $1; }
You need to wrap the bit you wish to extract in (paremphasis) and it'll b=
e put into $1. If you do two (wraps), you'll get $1 and $2, and so on. Or=
you can assign the extraction-match direclty: my ($one, $two) =3D $searc=
h_this =3D~ /(put_into_one)(put_into_two)$/.
--
> My goal is to print the last word.
> However, it only prints "Hooray! pattern found";
>=20
> Any idea what's wrong with $1??
>=20
> Thanks
>=20
>=20
>=20
>=20
>=20
> ________________________________________
__________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection=20
> around http://mail.yahoo.com=20
>=20
> --
> To unsubscribe, e-mail: beginners-unsubscribe@perl.org For=20
> additional commands, e-mail: beginners-help@perl.org=20
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>=20
>=20
>=20
http://www.bbc.co.uk/
This e-mail (and any attachments) is confidential and may contain persona=
l views which are not the views of the BBC unless specifically stated.
If you have received it in error, please delete it from your system.
Do not use, copy or disclose the information in any way nor act in relian=
ce on it and notify the sender immediately.
Please note that the BBC monitors e-mails sent or received.
Further communication will signify your consent to this.
=09
| |
| T Baetzler 2006-10-02, 3:58 am |
| Michael Alipio <daem0nb0y@yahoo.com> asked:
> #!/usr/local/bin/perl
> use strict;
>=20
> my $string =3D "Theres more than 1 way to do it"; if ($string=20
> =3D~ /\w+$/){ print "Hooray! pattern found"; print $1; }
>=20
> My goal is to print the last word.
> However, it only prints "Hooray! pattern found";
>=20
> Any idea what's wrong with $1??
Try "$string =3D~ /(\w+$)/" instead - you need the braces to
capture the value you want to print.
HTH,
Thomas
| |
|
| T Baetzler wrote:
> Michael Alipio <daem0nb0y@yahoo.com> asked:
To Michael Alipio: good !!!
[color=darkred]
To Michael Alipio: no use warnings; -- bad !!! -- Please turn on
warnings.
(I am usually very suspicious of programs that don't use warnings, but
in this particular case I grant you it would not have made any
difference)
[color=darkred]
As the motto is TIMTOWTDI...
[color=darkred]
>
> Try "$string =~ /(\w+$)/" instead - you need the braces to
> capture the value you want to print.
....I suggest another way to do it, without braces, using $&
if ($string =~ /\w+$/) {
print "Hooray! pattern found"; print $&;
}
Although I have to admit that [see perldoc perlvar] :
++ the use of this variable [ $&, as well as $` and $' ]
++ anywhere in a program imposes a considerable
++ performance penalty on all regular expression matches.
| |
| Sijo George 2006-10-03, 4:00 am |
| Else try This..
#!/usr/local/bin/perl
use strict;
my $string = "Theres more than 1 way to do it";
if ($string =~ /\w+$/){
print "Hooray! pattern found\n";
print $&;
}
Use the inbuilt perl variables..
$` prints the pre matching part, $& gives the matched variale and $' gives the
post matching part
Sijo
On Monday October 2 2006 1:16 pm, Michael Alipio wrote:
> Hi,
>
> Suppose I have this:
>
> #!/usr/local/bin/perl
> use strict;
>
> my $string = "Theres more than 1 way to do it";
> if ($string =~ /\w+$/){
> print "Hooray! pattern found";
> print $1;
> }
>
> My goal is to print the last word.
> However, it only prints "Hooray! pattern found";
>
> Any idea what's wrong with $1??
>
> Thanks
>
>
>
>
>
> ________________________________________
__________
> Do You Yahoo!?
> Tired of spam? Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
| |
| Paul Lalli 2006-10-03, 8:02 am |
| Sijo George wrote:
> #!/usr/local/bin/perl
> use strict;
>
> my $string = "Theres more than 1 way to do it";
> if ($string =~ /\w+$/){
> print "Hooray! pattern found\n";
> print $&;
> }
>
> Use the inbuilt perl variables..
> $` prints the pre matching part, $& gives the matched variale and $' gives the
> post matching part
These variables should *never* be used. Using these variables *once*,
anywhere in your script, causes a performance hit on *every* regular
expression in your entire program. Please read
peldoc perlvar
for more information.
Paul Lalli
|
|
|
|
|