Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: ^\s not equal \S?

by davido (Cardinal)
on Dec 04, 2003 at 08:46 UTC ( [id://312143]=note: print w/replies, xml ) Need Help??


in reply to ^\s not equal \S?

First, quantifiers inside character classes are not seen as quantifiers, but rather, as literal characters to become a part of the character class. Put your * quantifier outside of the character class if that's what you intend.

Next, [\S\W] means anything that's either a non-space character, or a non-word character (usually A-Za-z_). Well, just about everything is either a non-space or a non-word. In fact, since there is no overlap between \s and \w, you've just wiped out the entire line (especially with the /g modifier). Every character I can think of would either fit the "not space" or the "not word" catagory, and thus, every character is wiped out.

The second expression is a negated character class. You still need to get rid of those * quantifiers inside of the square brackets. The negated character class is saying any character that is not either a space or a word. That's different. The only characters that are neither space nor word, are things like comma, quote, (and many others).

So where your first regex matches everything, and substitutes it with nothing (thus wiping out the string), the second regex matches just characters that are neither space nor word, and substitutes those characters with nothing, leaving you with spaces and words.


Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://312143]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-03-28 17:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found