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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



kidd
User

Jun 24, 2002, 4:15 AM

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

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

Example


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

//something


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"


Jean
User


Jun 24, 2002, 5:57 AM

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

This will hopefully give you the idea...


Code
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
jean.spector@softhome.net


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


fashimpaur
User

Jun 24, 2002, 1:19 PM

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

kidd,

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:



Code
  



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



#also try


#my
$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";



$string
=~ 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,
Dennis

$a="c323745335d3221214b364d545".
"a362532582521254c3640504c3729".
"2f493759214b3635554c3040606a0",
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