Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Web CMS (TeamSite)
clearing $1
nipper
I am doing a regex, getting $1, $2, then another, but $1 and $2 are the old values.
If I comment out regex1, then regext 2 $1 $2 are fine
Here is a sample:
$txt="<match1 some stuff><match2 more stuff>";
$txt=~ m|.*<match1(.*?)>.*|g;
$match1=$1;
$txt=~ m|.*<match2(.*?)>.*|g;
$match2=$1;
print "match1 $match1 \n match2 $match2\n";
match1 some stuff
match2 some stuff
but when I comment out the 1st regex
print "here is $Image \n";
$txt="<match1 some stuff><match2 more stuff>";
# $txt=~ m|.*<match1(.*?)>.*|g;
# $match1=$1;
$txt=~ m|.*<match2(.*?)>.*|g;
$match2=$1;
print "match1 $match1 \n match2 $match2\n";
match1
match2 more stuff
So what am I doing wrong ?
Win2K TS 6.5 (iwperl)
Find more posts tagged with
Comments
jbonifaci
Just do something like this, it is cleaner:
my $txt = "<match1 some stuff><match2 more stuff>";
my ($match1) = ($txt =~ m|<match1\s+(.*?)>|);
my ($match2) = ($txt =~ m|<match2\s+(.*?)>|);
print "match1 $match1 \nmatch2 $match2\n";
~Jeff
Migrateduser
take the "g" modifier off, works as you're wanting to without that...
cheers,
-Rori
nipper
I used the g modifier in my real work, since $txt was very large. This was just a test case
to be certain I was not drunk.
Migrateduser
admittedly /g works a little different in matches vs. substitutions, but in general /g is used if you want multiple matches to be found...not because of the size of the string...so I guess the basic question is are you actually expecting more than one match to be found in your string?
nipper
In one of my cases I need to get multiple matches, so I will need it.
Migrateduser
this will work:
$_ = "<match1 some stuff><match2 more stuff><match1 third stuff><match2 fourth stuff>";
my
@first
= m|<match1(.*?)>|g;
my
@a
= m|<match2(.*?)>|g;
print "match1
@first
\nmatch2
@a\n"
;
outputs:
match1 some stuff third stuff
match2 more stuff fourth stuff
cheers,
-Rori
computedcolumn.rptdesign
rob1234
you should never use the dollar value without first checking that your match was succussful...
if ($test =~ m/(whatever.*)/) {
$match = $1; etc...