What is [^\s] in a regex supposed to mean? 
Author Message
 What is [^\s] in a regex supposed to mean?

I thought that `\s' was not special in [...].  But apparently:

/----

| Default die handler restored.
|
| Loading DB routines from perl5db.pl version 1.07
| Editor support available.
|
| Enter h or `h h' for help, or `man perldebug' for more help.
|
| main::(-e:1):   1
|   DB<1> $x = 'aaaasxx yyy';
|
|   DB<2> x $x =~ m/([^\s]+)/
| 0  'aaaasxx'
|   DB<3>
\----

... [^\s] matches all non-whitespace characters, rather than
everything except `s' or everything except `\' and `s'.

Is this supposed to be so?  Will it stay that way?  Which other \
constructs are allowed?

kai
--
~/.signature: No such file or directory



Tue, 02 Dec 2003 19:09:28 GMT  
 What is [^\s] in a regex supposed to mean?

Quote:

> ... [^\s] matches all non-whitespace characters, rather than
> everything except `s' or everything except `\' and `s'.

> Is this supposed to be so?

Apparently so. From `perldoc perlre`:

:                                                 You may use `\w', `\W',
:     `\s', `\S', `\d', and `\D' within character classes, but if you try to
:     use them as endpoints of a range, that's not a range, the "-" is
:     understood literally.

Quote:
> Will it stay that way?

I would suppose so, since it's documented.

Quote:
> Which other \ constructs are allowed?

See above.

It's all in the manpage! Er, that is, the one that says

: BUGS
:     This document varies from difficult to understand to completely and
:     utterly opaque. The wandering prose riddled with jargon is hard to
:     fathom in several places.

, unfortunately.

Cheers,
Philip
--

Yes, that really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.



Tue, 02 Dec 2003 22:00:52 GMT  
 What is [^\s] in a regex supposed to mean?

Quote:

> ... [^\s] matches all non-whitespace characters, rather than
> everything except `s' or everything except `\' and `s'.

> Is this supposed to be so?  Will it stay that way?

Yes, and yes.

Quote:
>  Which other \ constructs are allowed?

Try

  perldoc perlre

In short, never prepend alphanumeric characters with a backslash within
double quotes or regular expressions if you want them to be interpreted
literally.

Always escape non-alphanumeric by prepending a backslash if you are not
sure if they act as metacharacters in that context.

if you want to have a literal backslash, you will have to escape it.
[^\\s] will probably do what you want.
--
#!/usr/bin/perl -- -*     Nie wieder Nachtschicht!     *- -- lrep\nib\rsu\!#
use Inline C=>'void C(){int m,u,e=0;float l,_,I;for(;1840-e;putchar((++e>907

  =e%80)&&I*l+_*_<6&&26-++u;_=2*l*_+e/80*.09-1,l=I)I=l*l-_*_-2+m/27.;}';&C



Tue, 02 Dec 2003 21:55:11 GMT  
 What is [^\s] in a regex supposed to mean?

Quote:
> I thought that `\s' was not special in [...].  But apparently:

The m// constuct treats it's contents as a "" quoted string, so
things like "\n" have their normal meaning.  In addition, there
are some other "\<foo>" constructs just for regular expressions.

There're allwed in character classes ([...]), too.

Quote:
> ... [^\s] matches all non-whitespace characters, rather than
> everything except `s' or everything except `\' and `s'.

Yes.  This is because \s matches a whitespace character.

Quote:
> Is this supposed to be so?  

Yes.

Quote:
> Will it stay that way?  

I expect so.  It's true in all perl's I've seen.  But who knows what
Larry & friends will do for perl v10?

Quote:
> Which other \
> constructs are allowed?

see "man perlre".

But you might like to prefer to use \S rather than [^\s], as they're
equivilent, and \S is shorter.

--
Mike Bristow, seebitwopie  



Tue, 02 Dec 2003 22:31:10 GMT  
 What is [^\s] in a regex supposed to mean?

Quote:



>> ... [^\s] matches all non-whitespace characters, rather than
>> everything except `s' or everything except `\' and `s'.

>> Is this supposed to be so?

> Apparently so. From `perldoc perlre`:

> :                                                 You may use `\w',
> :     `\W', `\s', `\S', `\d', and `\D' within character classes, but
> :     if you try to use them as endpoints of a range, that's not a
> :     range, the "-" is understood literally.

I completely missed that part of the man page.  Thanks.

kai
--
~/.signature: No such file or directory



Wed, 03 Dec 2003 00:23:10 GMT  
 What is [^\s] in a regex supposed to mean?
[A complimentary Cc of this posting was sent to
Mike Bristow

Quote:
> > I thought that `\s' was not special in [...].  But apparently:

> The m// constuct treats it's contents as a "" quoted string, so
> things like "\n" have their normal meaning.

Wrong, otherwise there would be no need for qr//.  See "Gory details"
(which is slightly wrong too) for details.

Ilya



Wed, 03 Dec 2003 06:47:27 GMT  
 What is [^\s] in a regex supposed to mean?

Quote:

> In addition, there are some other "\<foo>" constructs just for
> regular expressions.

> There're allwed in character classes ([...]), too.

Not completely true. For example, \p{Prop} is available in regexes but
not in character classes.

Cheers,
Philip
--

That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.



Wed, 03 Dec 2003 14:56:29 GMT  
 What is [^\s] in a regex supposed to mean?

Quote:

> I completely missed that part of the man page.  Thanks.

So did I, at first reading, so I had another look at perlop, but of
course that said nothing about character classes. So I looked again at
perlre, thinking that it must be in there somewhere, and finally found
it.

But I'll agree that it was easy to miss; perlre is not completely a
model of clarity.

Cheers,
Philip
--

That really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.



Wed, 03 Dec 2003 14:56:36 GMT  
 What is [^\s] in a regex supposed to mean?

Mike> I expect so.  It's true in all perl's I've seen.  But who knows what
Mike> Larry & friends will do for perl v10?

I think it was added between Perl 2 and Perl 3 though.  So it's NOT been
in all the perl's *I* have seen.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Thu, 04 Dec 2003 02:26:01 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. serial port software

2. Limitations of readln?!?!?

3. I am the greatest regex artist on the planet

4. RegEx - Am I being ignored?

5. PerlQt, supposed to have a clean install?

6. What's a .ph supposed to do?

7. Is write FILEHANDLE supposed to set $= ?

8. Is "write FILEHANDLE" supposed to set $=

9. Is /PATTERN/io supposed to work?

10. don't quite get how refs are supposed to work

11. Novice Question: How is -e supposed to work?

12. Is this how formats are supposed to work?

 

 
Powered by phpBB® Forum Software