Re: regular expression help



On Mar 30, 2:51 pm, "bpatton" <bpat...@xxxxxx> 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
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

.



Relevant Pages

  • Re: %USERACL Is Empty?!
    ... On Fri, 9 Dec 2005, Paul Lalli wrote: ... >> basically %USERACL is a hash containing the key and values read from the ... >> sub initAuthMgr { ...
    (comp.lang.perl.misc)
  • Re: Adding values to anon hash of hash while looping...
    ... There was absolutely no trace of sarcasm when I wrote that. ... I hadn't realised I needed to declare the hash ... Paul Lalli ...
    (comp.lang.perl.misc)
  • Re: Question on File::Find
    ... Paul Lalli wrote: ... You didn't include the wanted option in your %findoption hash. ... what you think your push statement is doing ... sorry for the mistake, I presume I modified during posting, my mistake, ...
    (comp.lang.perl.misc)
  • Re: I need a host.
    ... > IF they allow clients to view error logs they do not provide ... > http://www.mamaloca.net/perl/ Hash and Hash with errors) the warnings ... just printing the names of the colors, ... Paul Lalli ...
    (perl.beginners)
  • Re: format output for loop help
    ... Paul Lalli wrote: ... So, obviously, s/he didn't bother to use strict; ... %MYHASH is a terrible name for a hash. ... now (or, God forbid, somebody else needs to modify your code). ...
    (perl.beginners)