in reply to Re: Re: Re: Is there an InString-like function?
in thread Is there an InString-like function?

As was already pointed out, this if ($var =~ /^[qw(Smith Jones Schmidtz)]/) doesn't work as it just creates a character class equivalent to [SmithJonescdz] - which just compares to the first character of $var. Two solutions:

my @names = qw(Smith Jones Schmidtz); my $pattern = join '|', @names; if ($var =~ /^(?:$pattern)/) # or quicker if ($var =~ /^Smith/ || $var =~ /^Jones/ || $var =~ /^Schmidtz/)

-- Hofmator

Replies are listed 'Best First'.
Re: Re4: Is there an InString-like function?
by Anonymous Monk on Jul 24, 2001 at 17:08 UTC
    so is /^(?:$pattern)/ faster than just /^($pattern)/

      so is /^(?:$pattern)/ faster than just /^($pattern)/
      Yes, it is. The normal parentheses capture the match inside into the variable $1 (or $2, $3, ...) and so they have to do extra work copying the characters. Both types of parentheses do grouping.

      But my solution used first an alternation inside the regex and then second a logical or to combine the result of different regexes. The result is the same but the second method is faster.

      -- Hofmator