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

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

Replies are listed 'Best First'.
Re^5: search of a string in another string with 1 wildcard
by AnomalousMonk (Archbishop) on Oct 12, 2014 at 20:57 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.

    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) };

        The regex definition  qr{ab[crdnfqm\+\s]def}; does add a  '+' character and the class of all whitespace characters to the  [crdnfqm] character class contained in the previous  qr{ab[crdnfqm]def}; definition. Note, however, that  m{ ($pattern) } (still) requires a blank (0x20) character before and after  $pattern in order to have a match. (NB: The  '+' character is not special, i.e., is not a metacharacter, inside a character class and so does not need the  \ escape — but it does no harm.)

        Please see perlre, perlrecharclass, perlrequick, and perlretut.