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

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.

Replies are listed 'Best First'.
Re^4: search of a string in another string with 1 wildcard
by carolw (Sexton) on Oct 12, 2014 at 16:57 UTC

    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.

Re^4: search of a string in another string with 1 wildcard
by carolw (Sexton) on Oct 12, 2014 at 18:18 UTC

    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.

        How to add + and space to the construct? So instead of

        my $pattern = qr{ab[crdnfqm]def}; my $pattern = qr{ab[crdnfqm\+\s]def};

        will be correct? to be used with your solution

        if $string =~ m{ ($pattern) };