Gossamer Forum
Home : General : Perl Programming :

Extracting from string

Quote Reply
Extracting from string
Hi all,

I have the following string in a file
<%fileage|fname=index.shtml%>
from this string I want extract fname=index.shtml. Then I have to split this expression into 2 vars containing fname and index.shtml. How can I do this.

Rene
Quote Reply
Re: Extracting from string In reply to
$string = "<%fileage|fname=index.shtml%>";
$string =~ s/[>%<]//g;
($left, $right) = split(/\|/,$string);
($fname, $file) = split(/=/,$right);

------------------
Chris
Quote Reply
Re: Extracting from string In reply to
That is what I searched for. Thanks a lot.

Rene
Quote Reply
Re: Extracting from string In reply to
#!/usr/bin/perl -w

use strict;

my $string = "<%fileage|fname=index.shtml%>";
$string =~ /<%.+\|(.+)=(.+)%>/;
my $var = $1;
my $file = $2;

print "$var value is $file";

--mark
Quote Reply
Re: Extracting from string In reply to
Hi Mark

Thanks for your solution, it works but only if I not write

use strict;

else I get an error. Do you know why?

Rene
Quote Reply
Re: Extracting from string In reply to
Yeah, don't use the use strict unless you set your whole program up to use that. Without setting up the whole program to use it, you'll get lots and lots of errors.

Leave out the use strict. You won't really need the my's either.

Just

$string =~ /<%.+\|(.+)=(.+)%>/;
$var = $1;
$file = $2;

will do for you.

--mark


[This message has been edited by Mark Badolato (edited January 29, 2000).]