Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

regular expression help

0 views
Skip to first unread message

bpatton

unread,
Mar 30, 2007, 2:51:02 PM3/30/07
to
I'm trying to set up some limitation by using regular expressions
recorded in a hash table.
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';

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.

Paul Lalli

unread,
Mar 30, 2007, 3:16:49 PM3/30/07
to

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

Purl Gurl

unread,
Mar 30, 2007, 3:20:52 PM3/30/07
to
bpatton wrote:

(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

Mumia W.

unread,
Mar 30, 2007, 4:53:43 PM3/30/07
to
On 03/30/2007 01:51 PM, bpatton wrote:
> I'm trying to set up some limitation by using regular expressions
> recorded in a hash table.
> 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';
>
> 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

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.

0 new messages