GS has asked for the wisdom of the Perl Monks concerning the following question:

Please help me understand the pattern below, just trying to learn pattern match

[[\\s(/]ssshd\\s
what is the use of first "[" in above pattern ? and also what is the use of "(" ?

Thanks a lot.

Replies are listed 'Best First'.
Re: Reg Exp
by toolic (Bishop) on Aug 27, 2010 at 19:34 UTC
    YAPE::Regex::Explain can help:
    use strict; use warnings; use YAPE::Regex::Explain; print YAPE::Regex::Explain->new('[[\\s(/]ssshd\\s')->explain(); __END__ The regular expression: (?-imsx:[[\s(/]ssshd\s) matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- [[\s(/] any character of: '[', whitespace (\n, \r, \t, \f, and " "), '(', '/' ---------------------------------------------------------------------- ssshd 'ssshd' ---------------------------------------------------------------------- \s whitespace (\n, \r, \t, \f, and " ") ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
      This points out an interesting issue with how the OP formatted his post. Because you fed YAPE::Regex::Explain a single-quote constructed string, it saw a whitespace character, whereas when I assumed the OP had wrapped the expression directly in m<...> I expected a backslash or an 's' in the class. I think your result is likely more logical.
Re: Reg Exp
by kennethk (Abbot) on Aug 27, 2010 at 19:03 UTC
    In general, most questions you might have about regular expressions can be found in perlre or perlretut.

    In regular expressions, square brackets indicate character classes. In this case you will match on any of the following list of characters:

    [ \ s ( /

    I'm actually a little surprised that the regular expression engine doesn't throw an error on an unescaped open square bracket in a character class, but it seems to have no problem parsing it, so...

    The rest of the expression just requires the literal string ssshd\s. Thus the following outputs 1:

    print '(ssshd\s' =~ m<[[\\s(/]ssshd\\s>;

      I would like to echo the first paragraph of the above post.   And please don’t treat it as an “RTFM!” brush-off, because it is not.

      perldoc pages are generally very well-written, and there are a great many of them which discuss regular expressions.   Some of them take a while to read, but it is time very well spent.   Regular expressions are one of the core power-tools of Perl, and these pages are the first and the best place to begin learning about them.