Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: ^\s not equal \S?

by davido (Cardinal)
on Dec 04, 2003 at 08:46 UTC ( #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
Node Status?
node history
Node Type: note [id://312143]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2020-10-30 02:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (277 votes). Check out past polls.

    Notices?