473,406 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Regular expressions and numerical only input

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

Nov 16 '06 #1
9 1624
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]*$");

--
SirMike - http://www.sirmike.org

C makes it easy to shoot yourself in the foot; C++ makes it harder, but
when you do, it blows away your whole leg. - Bjarne Stroustrup
Nov 16 '06 #2
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
Nov 16 '06 #3
I'd recommed to find the appropriate match there
http://regexlib.com/default.aspx

--
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche


"Trev" wrote:
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

Nov 16 '06 #4
Thanks - works beautifully!

Cheers

Trev

Nov 16 '06 #5
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]+)?$");

Nov 17 '06 #6
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

On 17 Nov 2006 03:49:11 -0800, "Trev" <t.*********@btinternet.com>
wrote:
>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]+)?$");
--

Bits.Bytes.
http://bytes.thinkersroom.com
Nov 17 '06 #7
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.

Nov 20 '06 #8

Trev wrote:
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 :(

Nov 20 '06 #9
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

On 20 Nov 2006 01:44:23 -0800, "Trev" <t.*********@btinternet.com>
wrote:
>
Trev wrote:
>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 :(
--

Bits.Bytes.
http://bytes.thinkersroom.com
Nov 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: YoBro | last post by:
Hi I have used some of this code from the PHP manual, but I am bloody hopeless with regular expressions. Was hoping somebody could offer a hand. The output of this will put the name of a form...
6
by: Scott Zuyderduyn | last post by:
Hi all, I've started planning a software project that utilizes XML to store a lot of different input from the user. Regular expressions comprise a portion of this input. I could store a regex...
11
by: Martin Robins | last post by:
I am trying to parse a string that is similar in form to an OLEDB connection string using regular expressions; in principle it is working, but certain character combinations in the string being...
6
by: Marc Scheuner [MVP ADSI] | last post by:
Folks, I've started using regular expressions for parsing some data string that come along - works quite nicely, however, as a newbie to RE, I'm still struggling with some special cases: 1) I...
9
by: Schorschi | last post by:
Not having used regular expressions much, I need some help. Given a string... "This\0Guy\0Needs\0Some\0Help\0\0\0\0\0" Need result as array of strings... "This","Guy", "Needs", "Some", "Help" ...
2
by: news.microsoft.com | last post by:
I need help design a reg exp. I am parsing an html file to get the input values, here is one example <input VALUE="Staff Writer" size=60 type="text" name="author"> Can I grab the value "Staff...
7
by: norton | last post by:
Hello, Does any one know how to extact the following text into 4 different groups(namely Date, Artist, Album and Quality)? - Artist - Album Artist - Album - Artist - Album - Artist -...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
47
by: Henning_Thornblad | last post by:
What can be the cause of the large difference between re.search and grep? This script takes about 5 min to run on my computer: #!/usr/bin/env python import re row="" for a in range(156000):...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.