simple regular expression give runtime error @"[.-\s]"

T

Tony Johansson

Hi!

Here is the pattern I use
string pattern = @"[.-\s]";
and the string is just "."

When I run this I get the runtime error
parsar [.-\s] - It's not possible to include the class \s in the character
interval.

If I instead move the dot(.) in the regular expression to a position after
the dash or as the last character it works fine .
[-.\s] or [-\s.]

So why is it not possible to have the dot(.) as the first character in the
regular expression ?

//Tony
 
K

Konrad Neitzel

Hi Tony!

Tony Johansson said:
Here is the pattern I use
string pattern = @"[.-\s]";
and the string is just "."
When I run this I get the runtime error
parsar [.-\s] - It's not possible to include the class \s in the character
interval.

What is a character interval?
If you want to get a charcter between a to z, you could simply list them all
or you can give an interval in the form a-z.

Why is \s not valid in an interval?
\s is already a list of characters. So it is not clear, which end-character
you mean. So your .-\s would mean all characters from . to \s but \s is a
space, \r, \n, \t or \f. So which is the end sign?
==> The error given explains it already.

When you put he - at the start, then it is no longer treated as an interval
(but maybe that depends on the implementation. I would expect an error in
that case, too!)
So normally I would expect something like \- if you want to have a - inside
the list of characters.

If you want to play around with regular expressions, you should read an
introduction to it. Or if you just prepare for the microsoft exam: In the
self-paced training book to the 70-536 I read, that regular expressions are
not part of the exam in depth. It is just required to know, how to use them
(e.g. IsMatch Method and so!) but it is not required to know how to build
complex regular expressions. (But it is a very powerfull tool so I would
recommend to read some more about it!)

With kind regards,

Konrad
 
T

Tom Shelton

Hi!

Here is the pattern I use
string pattern = @"[.-\s]";
and the string is just "."

When I run this I get the runtime error
parsar [.-\s] - It's not possible to include the class \s in the character
interval.

If I instead move the dot(.) in the regular expression to a position after
the dash or as the last character it works fine .
[-.\s] or [-\s.]

So why is it not possible to have the dot(.) as the first character in the
regular expression ?

//Tony

I'm not 100% what your trying to match, but my guess is your trying to create
a character class that matches the period, hyphen, or whitespace - and not one
that matches the range of any character to whitespace, which is what you have
there and why your getting the error.

The hyphen has special meaning inside of a character class when it is between
two other values - it becomes a range operator. For instance, [A-Za-z0-9]
matches any value from A to Z, a to z, or 0 to 9. To include the hyphen
literally, then you need to put it where it has no meaning, like at the
begining of the character class [-.\s] or [.\s-] or you need to escape it with
a backslash [.\-\s].

I also think you are trying to match the litteral period - so, you have to
escape it... [\.\-\s] or [-\.\s] or [\.\s-]

Any of those should match a single period, hyphen, or whitespace character.
 
T

Tony Johansson

Tom Shelton said:
Hi!

Here is the pattern I use
string pattern = @"[.-\s]";
and the string is just "."

When I run this I get the runtime error
parsar [.-\s] - It's not possible to include the class \s in the
character
interval.

If I instead move the dot(.) in the regular expression to a position
after
the dash or as the last character it works fine .
[-.\s] or [-\s.]

So why is it not possible to have the dot(.) as the first character in
the
regular expression ?

//Tony

I'm not 100% what your trying to match, but my guess is your trying to
create
a character class that matches the period, hyphen, or whitespace - and not
one
that matches the range of any character to whitespace, which is what you
have
there and why your getting the error.

The hyphen has special meaning inside of a character class when it is
between
two other values - it becomes a range operator. For instance, [A-Za-z0-9]
matches any value from A to Z, a to z, or 0 to 9. To include the hyphen
literally, then you need to put it where it has no meaning, like at the
begining of the character class [-.\s] or [.\s-] or you need to escape it
with
a backslash [.\-\s].

I also think you are trying to match the litteral period - so, you have to
escape it... [\.\-\s] or [-\.\s] or [\.\s-]

Any of those should match a single period, hyphen, or whitespace
character.

Good explained Tom!!
 

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

Similar Threads


Top