CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Regular Expressions:
Using m//



Jun 24, 2002, 4:15 AM

Post #1 of 3 (8500 views)
Using m// Can't Post

How can I use m// to check for something between special words.


$string = "sp A big string between sp\n sp Just fun sp"; 


And the result where "A big string between" "Just fun", so I got everything between sp.

Thanks for your help.

"So you want to be a cowboy
Well you know it's more than just a ride
Guess you got to know the real thing
If you want to know the other side
Ain't nobody riding shotgun
In this world tonight"


Jun 24, 2002, 5:57 AM

Post #2 of 3 (8500 views)
Re: [kidd] Using m// [In reply to] Can't Post

This will hopefully give you the idea...

my $string = "sp A big string between sp\n sp Just fun sp"; 
while ($string =~ /sp(.+?)sp/g) {
print "$1\n";

I'll try to explain the above m//:
() - grouping puts the match into $1 variable
+? - this is similar to just +, but uses the smallest match while + uses the longest possible
m//g - matches all the possiblities, not only the first one

I'm sure the explanation can be made much more readable, but that's the best I can do in 2 minutes

Jean Spector
SQA Engineer @ Exanet

There are only 10 types of people in the world -
Those who understand binary, and those who don't.


Jun 24, 2002, 1:19 PM

Post #3 of 3 (8496 views)
Re: [kidd] Using m// [In reply to] Can't Post


I see what Jean was trying to do, although I found it did not work in
certain situations. So, in an effort to give you a more reliable regex,
I came up with the following:


$string = "sp testing this regex sp\n sp isn't spelling fun sp";

#also try

$string = "sp an asp is a very dangerous snake sp\n sp don't let it bite sp";

# and the original,

#my $string = "sp A big string between sp\n sp Just fun sp";

=~ s/(^|\b)sp\b/~/gm;
while ($string =~ /~([^~]+)~/gm){
print $1."\n";

See if this doesn't handle words in the strings that start or end with 'sp'.

Good Luck,

print unpack"u*",pack "h*",$a,"\n\n";


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives