Use of \s versus space character

S

Smitty

I am wondering about the use of the \s regex construct within a string
vs using a space character.

For example if I wish to match this string
/Now is the time for all good men to come to the aid of their party/

Is there any advantage in using this regex
/^\w+(?:\s\w+)+$/

over this one
/^\w+(?: \w+)+$/ -- ie: s/\\s/ /g
 
S

Smitty

Bernard said:
Smitty said:
I am wondering about the use of the \s regex construct within a
string vs using a space character.


They are not the same. \s matches [\ \t\r\n\f], not just a space.

Correct, but that is not my question.

Is there any advantage (speed, perhaps ?) to /\s/ over / / in the
example I gave ?
 
A

Anno Siegel

Smitty said:
Smitty said:
I am wondering about the use of the \s regex construct within a
string vs using a space character.


They are not the same. \s matches [\ \t\r\n\f], not just a space.

Correct, but that is not my question.

Asking about the advantage of one construct over another only makes
sense if they both do the same thing. If they don't, choose the one
that expresses best what you want to do.
Is there any advantage (speed, perhaps ?) to /\s/ over / / in the
example I gave ?

Questions about speed are best answered on your own machine. Use the
Benchmark module, though I expect the difference will be insignificant.
If you need that kind of micro-optimization, Perl isn't your language
anyway.

Anno
 
S

Smitty

Anno said:
Asking about the advantage of one construct over another only makes
sense if they both do the same thing. If they don't, choose the one
that expresses best what you want to do.

Good point, need to get my thinking cap back on, I guess.
 
I

it_says_BALLS_on_your_forehead

Smitty said:
Bernard said:
Smitty said:
I am wondering about the use of the \s regex construct within a
string vs using a space character.


They are not the same. \s matches [\ \t\r\n\f], not just a space.

Correct, but that is not my question.

Is there any advantage (speed, perhaps ?) to /\s/ over / / in the
example I gave ?

I agree with Bernard--not the same thing. That being said, if you are
only trying to use \s for matching on the space character, it may be
faster to just use a space, since that the engine has to match on one
char versus a character class of 5. However, benchmarking is your
friend. In addition, if you are going to be matching on space, you
should really escape it with a backslash. this makes it more future
proof. I believe in Perl 6, the /x mode will be turned on by default.
 
S

Smitty

it_says_BALLS_on_your_forehead said:
..In addition, if you are going to be matching on space, you
should really escape it with a backslash. this makes it more future
proof. I believe in Perl 6, the /x mode will be turned on by default.

That's nice to know, thanks.
 
X

xhoster

Smitty said:
Bernard said:
Smitty said:
I am wondering about the use of the \s regex construct within a
string vs using a space character.


They are not the same. \s matches [\ \t\r\n\f], not just a space.

Correct, but that is not my question.

Your question made no sense.
Is there any advantage (speed, perhaps ?) to /\s/ over / / in the
example I gave ?

Is getting the right answer an advantage? Is getting the wrong answer
a disadvantage?

As for the speed, what happened when you tried it?

Xho
 
S

Smitty

Smitty said:
Bernard said:
I am wondering about the use of the \s regex construct within a
string vs using a space character.


They are not the same. \s matches [\ \t\r\n\f], not just a space.

Correct, but that is not my question.

Your question made no sense.

Sure it did, you probably meant to say that you didn't understand it.
Is getting the right answer an advantage? Is getting the wrong answer
a disadvantage?

As for the speed, what happened when you tried it?

I could not decide how to construct a test which gave me meaningful
results without taking into account how frequently my process got a
time slice. At that point I decided it probably wasn't worth worrying
about, but thought I would ask anyway.
 
P

Paul Lalli

Smitty said:
Smitty said:
Bernard El-Hagin wrote:

I am wondering about the use of the \s regex construct within a
string vs using a space character.

They are not the same. \s matches [\ \t\r\n\f], not just a space.

Correct, but that is not my question.

Your question made no sense.

Sure it did, you probably meant to say that you didn't understand it.

Mmm, no, I'm guessing Xho, and most of us, understood your question
perfectly. In fact, it was understood so well as to realize that it
was nonsensical. You might just as well have asked "Is there any
advantage of printing 'Hello World' over multiplying $foo by $bar?"
They do different things. It makes no sense to ask if one has an
advantage over the other. The only advantage is "one might work where
the other fails, for some given dataset." Which one works and which
one fails (if either) cannot be known without knowing the problem
description in question.
I could not decide how to construct a test which gave me meaningful
results without taking into account how frequently my process got a
time slice.

perldoc Benchmark

Paul Lalli
 
S

Smitty

Paul said:
Smitty said:
Bernard El-Hagin wrote:

I am wondering about the use of the \s regex construct within a
string vs using a space character.

They are not the same. \s matches [\ \t\r\n\f], not just a space.

Correct, but that is not my question.

Your question made no sense.

Sure it did, you probably meant to say that you didn't understand it.

Mmm, no, I'm guessing Xho, and most of us, understood your question
perfectly. In fact, it was understood so well as to realize that it
was nonsensical.

Appearantly not, but I'll get to that later.
You might just as well have asked "Is there any
advantage of printing 'Hello World' over multiplying $foo by $bar?"

Well, no, this is completely different, perhaps a better analogy would
be:
Is there any advantage to asking a person holding a basket that you
know to contain fruit, "Does your basket contain an apple, or a pear,
or a peach or a banana" as apposed to asking "Does your basket contain
an apple". Clearly in this case if you are only interest in knowing if
the basket contains an apple, you would ask so. And so it *probably*
is with perl and the difference between /\s/ and / /
It makes no sense to ask if one has an advantage over the other.

Well, let's think about that statement.

Given that I do not have any interest in reviewing the source code to
the perl regex engine, is it reasonable to think that perhaps there is
a possibility that the regex engine might perform some query
optimisation on /\s/ which might not happen with / / ?

Or maybe the construct (?:\s\w+)+ is somehow more efficiently processed
than (?: \w+)+

You see, I don't know, and so my question

Is there any advantage in using this regex
/^\w+(?:\s\w+)+$/

over this one
/^\w+(?: \w+)+$/ -- ie: s/\\s/ /g

is very reasonable, and sensible

perldoc Benchmark

Yes, I read about that, but I was put off bothering with it when I read
such caveats as
"Short tests may produce negative figures..." and "...CPU scheduling
and other operating system factors may complicate the attempt ..."

Which is pretty much what I was alluding to in my comment above.
 
X

xhoster

Smitty said:
I could not decide how to construct a test which gave me meaningful
results without taking into account how frequently my process got a
time slice.

If uncertainty as to how frequently your process gets a time slice renders
an experimental test irrelevant, then it renders a theoretical dissertation
even more irrelevant.

Xho
 
S

Smitty

If uncertainty as to how frequently your process gets a time slice renders
an experimental test irrelevant, then it renders a theoretical dissertation
even more irrelevant.

Xho

I am so glad that you understand the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top