$string = '-Dz=a =Dy=b -Dx=c -Dw=d -Dv=e -Du=f -Dt=g -Ds=h';
For an or I can
$search = '-Dx=(c|g|h)'; # this came from hash value
If ($string =~ /$search/) { # This works fine
...
}
Now how would I do an 'and' in any order
and
V
$search = '-Ds=(a|b|c)&&-Du=(f|g|h)'; # need both -Du and -Ds in any
order
If ($string =~ /$search/) {
...
}
It's probably very simple, just can't twist my mind around it :)
I can't separate the $search. It must always be a single string.
I would suggest fixing your algorithm to make more sense, so that it
doesn't hog tie you from using methods this messy, but until then...
if ($string =~ /Ds=[abc].*Du=[fgh]|Du=[fgh].*Ds=[abc]/) { ... }
Paul Lalli
(snipped)
> How do I do an 'and' in regular expressions?
> $string = '-Dz=a =Dy=b -Dx=c -Dw=d -Dv=e -Du=f -Dt=g -Ds=h';
> $search = '-Dx=(c|g|h)'; # this came from hash value
> If ($string =~ /$search/) { # This works fine
> Now how would I do an 'and' in any order
> $search = '-Ds=(a|b|c)&&-Du=(f|g|h)'; # need both -Du and -Ds in any
> I can't separate the $search. It must always be a single string.
This is untrue.
#!perl
$string = "-Ds=a-Du=h";
$search = '-Ds=(a|b|c)&&-Du=(f|g|h)';
($search1, $search2) = split (/&&/, $search);
if (($string =~ /$search1/) && ($string =~ /$search2/))
{ print "match"; }
PRINTED RESULTS:
match
Purl Gurl
Here's a sloppy but easy way:
$search = '-D[us]=([abcfgh])'
This is less sloppy:
$search = '-Ds=([abc])|-Du=([fgh])'
> If ($string =~ /$search/) {
> ....
> }
>
> It's probably very simple, just can't twist my mind around it :)
>
> I can't separate the $search. It must always be a single string.
>
I assumed that you wanted to actually capture the a|b|c or f|g|h rather
than just use alternation. If you don't need to capture anything, you
can eliminate the parentheses.