regular expression help

B

bpatton

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.
 
P

Paul Lalli

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.

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
 
M

Mumia W.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top