Help - Search - Member List - Calendar
Full Version: Regular expressions
WorkTheWeb Forums > Webmaster Resources > Perl Beginner Help
Support our Sponsors!
Steve Hemond
Hi again,

I want to make a search on two words.

If 'one two' is found, it is okay.
If 'one' is found, it is incorrect.
If 'two' is found, it is also incorrect.

I want the search to return me occurences of 'one two' found together.

I am searching this way :
if ($text =~ /one two/)

What I am doing wrong?

Thanks,

Best regards,

Steve Hemond
Programmeur Analyste / Analyst Programmer
Smurfit-Stone, Ressources Forestieres
La Tuque, P.Q.
Tel.: (
[Email Removed]

James Edward Gray II
On Dec 17, 2003, at 9:59 AM, Hemond, Steve wrote:

QUOTE
Hi again,

I want to make a search on two words.

If 'one two' is found, it is okay.
If 'one' is found, it is incorrect.
If 'two' is found, it is also incorrect.

I want the search to return me occurences of 'one two' found together.

I am searching this way :
if ($text =~ /one two/)

What I am doing wrong?

Nothing that I can see. Although, /bone twob/ is probably slightly
better. What's the problem?

James

Drieux
On Dec 17, 2003, at 7:59 AM, Hemond, Steve wrote:
[..]
QUOTE
I am searching this way :
if ($text =~ /one two/)

What I am doing wrong?

ok, I bite, what is the problem?

given
#!/usr/bin/perl -w
use strict;

while ( <DATA> ) {
my $text = $_;
if ($text =~ /one two/)
{
print "line is ok:nt$text";
} else {
print "WRONG!nt$text";
}
}

__DATA__
If 'one two' is found, it is okay.
If 'one' is found, it is incorrect.
If 'two' is found, it is also incorrect.

we get
line is ok:
If 'one two' is found, it is okay.
WRONG!
If 'one' is found, it is incorrect.
WRONG!
If 'two' is found, it is also incorrect.

What Problem?


ciao
drieux

---

Thomas Btzler
April T.Barrett <[Email Removed]> asked:
QUOTE
I need some tips on going about regular expression:

parsing through a text file and printing all of the words
that - for example - Begin and end with an "G"

Sample text courtesy of Project Gutenberg:

#!/usr/bin/perl -w

while( my $line = <DATA> ){
while( $line =~ m/W((gw*?)|(w*?g))W/ig ){
print "Line $.: $1n";
}
}

__DATA__

Puck's Song


See you the dimpled track that runs,
All hollow through the wheat?
O that was where they hauled the guns
That smote King Philip's fleet!

See you our little mill that clacks,
So busy by the brook?
She has ground her corn and paid her tax
Ever since Domesday Book.

See you our stilly woods of oak,
And the dread ditch beside?
O that was where the Saxons broke,
On the day that Harold died!

See you the windy levels spread
About the gates of Rye?
O that was where the Northmen fled,
When Alfred's ships came by!

See you our pastures wide and lone,
Where the red oxen browse?
O there was a City thronged and known,
Ere London boasted a house!

And see you, after rain, the trace
Of mound and ditch and wall?
O that was a Legion's camping-place,
When Caesar sailed from Gaul!

And see you marks that show and fade,
Like shadows on the Downs?
O they are the lines the Flint Men made,
To guard their wondrous towns!

Trackway and Camp and City lost,
Salt Marsh where now is corn;
Old Wars, old Peace, old Arts that cease,
And so was England born!

She is not any common Earth,
Water or Wood or Air,
But Merlin's Isle of Gramarye,
Where you and I will fare.

Ing. Branislav Gerzo
Thomas Btzler [TB], on Wednesday, July 13, 2005 at 12:38 (+0200) made
these points:

QUOTE
that - for example - Begin and end with an "G"
TB>  while( $line =~ m/W((gw*?)|(w*?g))W/ig ){


this will print OR, he wants AND, so, change this regexp to:
m/W((g)w*2)W/ig

--

How do you protect mail on web? I use http://www.2pu.net

[Today is a good day to bribe a high--ranking official.]

Arjun Mallik
Hi ,
Use the expression /^G.*G$/

^ ----> Denotes the beginning of the line
$ -----> denotes ending of the line


if u dont want case sensitivity


use /^G.*G$/i


i --> ignores case sensitivity

Arjun

Deserve before you desire







-----Original Message-----
From: April T.Barrett [ <mailto:[Email Removed]>
mailto:[Email Removed]]
Sent: Wednesday, July 13, 2005 3:49 AM
To: [Email Removed]
Subject: Regular Expressions


Hello:



I need some tips on going about regular expression:



parsing through a text file and printing all of the words that - for
example -

Begin and end with an "G"



Thanks.






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







Confidentiality Notice


The information contained in this electronic message and any attachments to this message are intended
for the exclusive use of the addressee(s) and may contain confidential or privileged information. If
you are not the intended recipient, please notify the sender at Wipro or [Email Removed] immediately
and destroy all copies of this message and any attachments.


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.