in reply to a regex which can detect if a string have all of some characters

Lookaheads do the tricks, but I wouldn't call them "concise".
#!/usr/bin/perl use strict; use warnings; use Test::More qw(no_plan); my @a = ('a','r','z','x'); my $re = join '', map { "(?=.*?$_)" } @a; like 'xzarfoobar', qr{$re}; unlike 'zarfoobar', qr{$re};

(Update: formulated as test script)

  • Comment on Re: a regex which can detect if a string have all of some characters
  • Download Code

Replies are listed 'Best First'.
Re^2: a regex which can detect if a string have all of some characters
by ikegami (Patriarch) on May 13, 2008 at 21:27 UTC
    Safer with just a few extra chars: map { "(?=.*?\Q$_\E)" }
      .. or my favorite formulation:
      my $re = join '', map { "(?=.*?$_)" } map { quotemeta } @a;