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: Beginner:
Find text betwin 2 strings in a string

 



patrik
stranger

Jul 8, 2001, 1:47 AM

Post #1 of 5 (619 views)
Find text betwin 2 strings in a string Can't Post

Ok, I know Im being a jackas with all dumd questions
here. But this is the only good place I know asking about
PERL related stuff.....

I got a string looking something like:
X-X9X-XSome OtherText Goes Here
or:
X-X597X-XSome OtherText Goes Here

How do find the text betwin X-X and X-X and put it
in a string for later use ?

Thanx in advance (again)
Patrik

Excuse the bad english
Im from Sweden ;)


mhx
Enthusiast / Moderator

Jul 8, 2001, 2:35 AM

Post #2 of 5 (616 views)
Re: Find text betwin 2 strings in a string [In reply to] Can't Post

Hi patrik,

don't fear to ask ;-)
Use this regex match to get the text between the two X-X's:

Code
my $string = "X-X597X-XSome OtherText Goes Here"; 
my ($between) = $string =~ /X-X(.*?)X-X/;

Hope this helps.

-- Marcus



patrik
stranger

Jul 8, 2001, 3:11 AM

Post #3 of 5 (614 views)
Re: Find text betwin 2 strings in a string [In reply to] Can't Post

Why wont this work ?
$between = $xline =~ /$a_str(.*?)$v5/;

I think its the $a_str and the $v5 strings that messes it up, but why ?

Excuse the bad english
Im from Sweden ;)


mhx
Enthusiast / Moderator

Jul 8, 2001, 3:57 AM

Post #4 of 5 (613 views)
Re: Find text betwin 2 strings in a string [In reply to] Can't Post

Hi patrik,

your $between is 1 after that line, heh?
The problem is not the $a_str and the $v5, the problem is you have to call the regex match in list context to make it return the catched text. Just put brackets around the $between variable:

Code
($between) = $xline =~ /$a_str(.*?)$v5/;

In scalar context (as you put it), the regex match returns a boolean indicating success.
Hope this helps.

-- Marcus



patrik
stranger

Jul 8, 2001, 5:00 AM

Post #5 of 5 (612 views)
Re: Find text betwin 2 strings in a string [In reply to] Can't Post

Didnt think of that =)

Loads of Thanx.

/Patrik

Excuse the bad english
Im from Sweden ;)

 
 


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

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