in reply to Re: search of a string in another string with 1 wildcard
in thread search of a string in another string with 1 wildcard

I would like to slightly change the question:

How to modify the code, exactly '.' if the pattern to be matched is a fixed string and one character is not any character but can be a character in a set of characters at the same position:

$pattern = 'abcdef';

at the 3rd position, c could only be replaced by any character in the set {r,d,n,f,q,m}, for ex.

  • Comment on Re^2: search of a string in another string with 1 wildcard

Replies are listed 'Best First'.
Re^3: search of a string in another string with 1 wildcard
by AnomalousMonk (Archbishop) on Oct 12, 2014 at 16:03 UTC

    One way:

    c:\@Work\Perl>perl -wMstrict -le "my $string = 'abcdef'; ;; my $pattern = qr{ [rdnfqm] }xms; ;; print qq{matched '$1' at offset $-[1]} if $string =~ m{ ($pattern) }xms; " matched 'd' at offset 3
    The construct  [rdnfqm] defines a "character class". Please see perlre, perlrequick, and perlretut.

      How do you find this?

      my @matches = ( qr/abcdef/, qr/abfdef/,#for simplicity, limited to 2 choices ); if ($line ~~ @matches){ ... }

        Perhaps:

        c:\@Work\Perl>perl -wMstrict -le "my @matches = (qr/abcdef/, qr/abfdef/); ;; for my $line (qw(xxxx abcd abcdef xabcdefx xabcdefxabfdefx)) { if ($line ~~ @matches){ print qq{match for '$line'}; } else { print qq{NO match: '$line'}; } } " NO match: 'xxxx' NO match: 'abcd' match for 'abcdef' match for 'xabcdefx' match for 'xabcdefxabfdefx'

        Please see docs on smart matching in perlsyn or perlop depending on your Perl version. (Please also note that there have been second thoughts about smart matching and it is now considered 'experimental': proceed at your own risk.

      in fact, your code shouldn't be some thing like

      my $pattern = qr{ab[crdnfqm]def}; if $string =~ m{ ($pattern) }; ....

      which doesn't find all strings with the pattern.

      I don't understand trailing xms at the end of pattern.

      I think you should forget my previous solution as the user should type all the qr which is not convenient

        in fact, your code shouldn't be some thing like
            my $pattern = qr{ab[crdnfqm]def};
                if $string =~ m{ ($pattern) };
                         ....
        which doesn't find all strings with the pattern.

        That is perhaps because the regex  m{ ($pattern) } (which lacks the  /x regex modifier) requires a space before and after  $pattern in order to match.

        I don't understand trailing xms at the end of pattern.

        These are regex modifiers. The  /x modifier makes the regex compiler ignore whitepace (for improved readability). The  /m and  /s modifiers control the behavior of, respectively, the  ^ $ (string end) and  . (dot) metacharacter operators. I have adopted the best practice recommended by TheDamian of always (well, almost always) including these modifiers in every  qr// m// s/// regex I write. The reason for this is to improve regex readability (/x) and to make explicit the behavior of the  ^ $ . operators (/ms).

        Please see perlre, perlrequick, and perlretut.