in reply to Match a chunk

I, too, cannot understand what you are trying to accomplish by using the /g regex modifier or, in a broader sense, what your ultimate goal may be. If you could give us a better idea of this goal, we might be better able to help. I suspect this may be an instance of an XY Problem.

One thing strikes me about the 'reverse' regex approach you are using. If $s1 and $s2 are both 'pure' strings with no regex metacharacters (or with any metacharacters escaped), then the expression  $s1 =~ /$s2/ is true if $s2 is a substring of $s1. If this is true and the match is reversed,  $s2 =~ /$s1/ is true if $s2 equals $s1 by string comparison (i.e.,  $s2 eq $s1 is true).

So why not just use index (to find a substring) and eq (see the string Equality Operators) instead of trying to deal with the arcana of the operation of /g across multiple regexes?

Replies are listed 'Best First'.
Re^2: Match a chunk
by dhasaan (Initiate) on Jun 13, 2008 at 13:36 UTC
    Let me try to express the problem better, i did indeed confuse by including too much. Being less confused myself this morning, i have eliminated all except what i really need in the following and resolved it:
    #!/usr/bin/perl -w use strict; my @updateloc1 = ("LakeOsouth/stuf", "LakeOeast/stuf", "LakeO2/stuf", +"LakeO/stuf"); my @texlines = ("LakeOsouth", "LakeOeast", "LakeO2", "LakeO"); for my $lin (@texlines){ for my $update (@updateloc1){ if ($update =~ /$lin/){ print "The table column $lin matches the directory name $u +pdate, so now do stuf in the subdir\n"; } } }
    The above code works, except that when it gets to table column $lin = LakeO it matches all the members in updateloc1, so my struggle has been to make $lin match $update exactly to the forward slash. Well, after re-arranging the loops, the solution is simple: $update =~ /$lin\//. Should have taken a break much sooner.