You are viewing a plain text version of this content. The canonical link for it is here.
Posted to regexp-user@jakarta.apache.org by Frot <fr...@earthling.net> on 2001/09/23 23:43:58 UTC

Detecting whitespace

Hi,

Have a short question concerning the regular expression for detecting a
'empty' line (e.g. no chars or only whitspace).

In RE this would be "(^$)|[\s]+"   ...... or ??

Unfortunatly the \s part does not compile under JAVA !!!
Using \\s does not work since the regular expression does not work as expected.

Any idea's ???

Fred

P.S. I will create a example prog. later to show what I mean...



Re: Detecting whitespace

Posted by Alexander Anderson <a....@utoronto.ca>.
Sun, Sep 23, 2001 at 03:10:11 PM, Jon Stevens wrote:
> > Using \\s does not work since the regular expression does not work as
> > expected.
> 
> \\\s

That would result it a compilation error. To explain it in detail,
string "\\\s" consists of two characters: '\\' and '\s'. Character '\\'
is a backslash. Character '\s' is an invalid escape character.

Let's go back to the original regexp: "(^$)|\s+". To me it says: match
lines that are either empty lines or *containing* whitespace, which is not
what Fred really wanted, i.e.:

> > the regular expression for detecting a 'empty' line (e.g. no chars or
> > only whitspace).

This regular expression could be: "^\s*$". That is, match all lines
consisting of 0 or more whitespace characters.

Now, to represent this string is Java code, all special characters need to
be escaped by a backslash. So the final answer is:

new RE("^\\s*$");

That's all.

Re: Detecting whitespace

Posted by Jon Stevens <jo...@latchkey.com>.
on 9/23/01 2:43 PM, "Frot" <fr...@earthling.net> wrote:

> Using \\s does not work since the regular expression does not work as
> expected.

\\\s

-jon