in reply to Regular Expression Question

Once you've got your regex that you will be using to determine if a vowel/consonant combo is valid in your dictionary file, I would either pre-compile it using:

my $regex = qr/$string/; while (my $word = <DICT>) { chomp($word); if ($word =~ $regex) { $yep++; } }

or use the /o modifier (which assures the compiler that you won't be changing the contents of $string, or at least tells it not to bother recompiling):

while (my $word = <DICT>) { chomp($word); if ($word =~ /$string/o) { $yep++; } }

so that the regex won't be recompiled each time, as since there are variables in the regex, it forces a recompile, which will slow down your app quite a bit.

Just my two cents.

Replies are listed 'Best First'.
Re: Re: Regular Expression Question
by Anonymous Monk on Apr 04, 2003 at 08:27 UTC
    That seems like it would work, however, It will still not match the pattern. The regex doesn't seem to be working correctly using the code above. And I believe you need quotes around my $regex = "qr/$string/"; Like so. Lets say you have this:
    my $regex = "/[aeiou][aeiou]/";
    It tries to match the literal slash then bracket and so on..., not using it as a reg. ex. at all. I think it is taking the pattern to match literally, as in it is trying to match. If you leave off the quotes and you print the string, it isn't right.

      qr// is its own operator, and will not work with double quotes around it. If your regex variable ($string, in this case) that you will be pre-compiling has slashes in it, then of course it will try to match the slashes. It is assumed that the only thing inside the qr// operator is the regular expression itself. The previous posts before mine didn't contain slashes in their variable, either.

        Thank you perlguy for all your help!! I got it to work now. And again, thanks for the prompt attention!
        P0werz