Break it down to its components. [] builds a character set. Inside you've got \w and \s, which would normally match a word character or a space character. You've got an extra backslash in your question which isn't present in the cookbook regular expression. So get rid of that -- it's not really part of your intended question.

Now, we've just about got it figured out, except there's that pesky ^ character, which turns out to negate the character class. So now it doesn't match words and spaces. Now it will match anything that is NOT a word or space character.

The final step is the +, which tells perl to match one or more times. So it must match at least one or more characters that are NOT word or space characters.

Word characters are usually A-Za-z_0-9 (A to Z caps and lowers, plus underscore and digits zero through nine). Space characters are what we generally think of as 'whitespace'.

So to summarize, match any non-word or non-space character, and match as many as possible with a minimum of one.

Update: Are you sure that's exactly the example from the Perl cookbook? I don't have my old copy handy, but that sub dequote; right at the beginning looks odd to me, though it's not actually a problem. It just seems odd given the context.

Update 2: You could also use YAPE::Regex::Explain to check for an explanation yourself. ...or you could use it to verify the accuracy of my description. ;).

use YAPE::Regex::Explain; print YAPE::Regex::Explain->new('[^\w\s]+')->explain();

And the relevant output:

[^\w\s]+ any character except: word characters (a- z, A-Z, 0-9, _), whitespace (\n, \r, \t, \f, and " ") (1 or more times (matching the most amount possible))

Ta-da!


Dave


In reply to Re: a match question by davido
in thread a match question by jinqiyi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.