Regular expressions and numerical only input

T

Trev

Hi everyone,

I'm having some problems with my regular expressions in C# and was
wondering if anyone could help?

Basically, what I'm trying to do is check for numerical only input -
either integer, or double,
so I need to check for 1234, 1234.5 etc.

The integer check I think is as follows:
Match m = Regex.Match( MyString, "^[0-9]*$");

but I'm having problems checking for anything with a decimal point in
it
Match m = Regex.Match( MyString, "^[0-9]*/\.{0,1}/[0-9]*$");

- the compiler grumbles about unknown tokens and syntax errors.

TIA

Paul
 
S

SirMike

Dnia 16 Nov 2006 03:42:43 -0800, Trev napisa³(a):
Match m = Regex.Match( MyString, "^[0-9]*/\.{0,1}/[0-9]*$");

try
Match m = Regex.Match( MyString, @"^[0-9]*/\.{0,1}/[0-9]*$");
 
H

Hans Kesting

Hi everyone,
I'm having some problems with my regular expressions in C# and was
wondering if anyone could help?

Basically, what I'm trying to do is check for numerical only input -
either integer, or double,
so I need to check for 1234, 1234.5 etc.

The integer check I think is as follows:
Match m = Regex.Match( MyString, "^[0-9]*$");

but I'm having problems checking for anything with a decimal point in
it
Match m = Regex.Match( MyString, "^[0-9]*/\.{0,1}/[0-9]*$");

- the compiler grumbles about unknown tokens and syntax errors.

TIA

Paul

the "\" is special within strings for the C# compiler, unless escaped.
Use:

Match m = Regex.Match( MyString, "^[0-9]+(\\.[0-9]+)?$");
or
Match m = Regex.Match( MyString, @"^[0-9]+(\.[0-9]+)?$");

this will match:
123
123.5
0.3

and not match
123.
..6



Hans Kesting
 
T

Trev

If I wanted to check for only integers, would this work? I've put
\.{0}, which I think means
"match the decimal point zero times exactly"

m = Regex.Match( args[0].ToString(), @"^[0-9]+(\.{0}[0-9]+)?$");
 
R

Rad [Visual C# MVP]

Hey Trev,

Here are a couple of regexes to help you out

^\d+$

This one will match any whole number with no decimals

So it will match,1, 5, 100, 600.

^\d+(?:\.\d+)*$

This one will match any whole number as well as any number with
decimals

So it will match 1, 5, 100, 600, 5.5, 80.3 and 3.151512924

If I wanted to check for only integers, would this work? I've put
\.{0}, which I think means
"match the decimal point zero times exactly"

m = Regex.Match( args[0].ToString(), @"^[0-9]+(\.{0}[0-9]+)?$");
 
T

Trev

Thanks mate, got that sorted out, cheers, no probs!

Still having some other regex woes though. If I wanted to check for "a
is b and c is d"
where a and c can be X,Y,Z and b and d can be N,S,E,W,D,U (eg "Y is N
and Z is U")
what expression should I use?

m = Regex.Match( args[0].ToString(),
@"(/X|Y/Z/)(\s)(/is/)\2(/N|E|W|S|D|U/)\2(/and/)\1\2\3\2\4");

doesn't work :(
bummer.
 
T

Trev

Trev said:
Thanks mate, got that sorted out, cheers, no probs!

Still having some other regex woes though. If I wanted to check for "a
is b and c is d"
where a and c can be X,Y,Z and b and d can be N,S,E,W,D,U (eg "Y is N
and Z is U")
what expression should I use?

m = Regex.Match( args[0].ToString(),
@"(/X|Y/Z/)(\s)(/is/)\2(/N|E|W|S|D|U/)\2(/and/)\1\2\3\2\4");

doesn't work :(
bummer.

Spotted a mistake. It should be
m = Regex.Match( args[0].ToString(),
@"(/X|Y|Z/)(\s)(/is/)\2(/N|E|W|S|D|U/)\2(/and/)\1\2\3\2\4");

- but it still doesn't want to play :(
 
R

Rad [Visual C# MVP]

Hey Trev,

Try this one

[X-Z]{1} is [NSEWDU]{1} and [X-Z]{1} is [NSEWDU]{1}

It matches these ones

X is N and X is W
Y is S and Y is D
Z is E and Z is U

Thanks mate, got that sorted out, cheers, no probs!

Still having some other regex woes though. If I wanted to check for "a
is b and c is d"
where a and c can be X,Y,Z and b and d can be N,S,E,W,D,U (eg "Y is N
and Z is U")
what expression should I use?

m = Regex.Match( args[0].ToString(),
@"(/X|Y/Z/)(\s)(/is/)\2(/N|E|W|S|D|U/)\2(/and/)\1\2\3\2\4");

doesn't work :(
bummer.

Spotted a mistake. It should be
m = Regex.Match( args[0].ToString(),
@"(/X|Y|Z/)(\s)(/is/)\2(/N|E|W|S|D|U/)\2(/and/)\1\2\3\2\4");

- but it still doesn't want to play :(
 

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

Top