in reply to Set theory in regexs

I tend to get suspicious when I see stuff like:
This problem must be solved with a regex (for other reasons).
Because it means you are artificially restricting the implementation to something that doesn't work well, except when you are toying with the language instead of doing real work with it. Often the case with doing homework, but I won't go that far to accuse you of doing a homework problem using the resources of the monestary. {grin}

The problem you ask is not solved well by a regex. Certainly, you can see if a string has both "d4" and "b2" in it using two forward lookaheads:

my $good = $string =~ /^(?=.*?(\s|^)d4(\s|$))(?=.*?(\s|^)b2(\s|$))/;
But this is not particularly efficient, nor will any solution be efficient if you restrict your implementation to "must be solved with a regex".

Please explain the need for a regex.

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
RE: Re: Set theory in regexs
by dcorbin (Sexton) on Sep 02, 2000 at 21:16 UTC
    The need for a regex solution is as you surmise, partially arbirtrary (no, I'm not doing homework, it's been 15+ years since I had any to do). The problem is that this particular problem is only a small portion of something I'm trying to solve as part of a large regex. I have considered other non-regex solutions, and I may end up returning to them, but there will be certain grace to solving the "big problem" with regex if practical.

    As for inefficiency, the question becomes "how inefficient?". While what I hope to do is construct a "very massive" regex, If it can be applied to a 2K string and resolved in under a minute on single processer Intel box, that's probably fast enough for my purposes. (I realize that the question "How inefficient? is difficult to answer without a far better understanding of the entire pattern and the input, but I thought I'd ask anyway :)

    I've never used lookaheads, so I'll have to "do some research" to understand your example.

    Thanks.