473,402 Members | 2,055 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,402 software developers and data experts.

Matching a number

Hi,

I'd like to validate a number input using a regular expression. This
would be done by "\d*". Now the input may have a fdecimal separator,
wich would be matched by "\d*(\.|,)\d*" (allowing the , as separator as
well).

What I need know is to ensure, that the total length of the string does
match a fixed length. Without the decimal separator this is trivial, but
with decimal separator I am stuck, as I am not too deep into regular
expression syntax.

TIA,
Tobi
Aug 23 '07 #1
5 1534
On Aug 23, 4:13 pm, Tobias Schröer <tobias-schroeerNOS...@gmx.de>
wrote:
I'd like to validate a number input using a regular expression.
Does it *have* to be a regular expression? Are you doing this using
some component which requires a regular expression, or could you split
it into a regular expression and a length check?

Jon

Aug 23 '07 #2
Jon Skeet [C# MVP] schrieb:
On Aug 23, 4:13 pm, Tobias Schröer <tobias-schroeerNOS...@gmx.de>
wrote:
>I'd like to validate a number input using a regular expression.

Does it *have* to be a regular expression? Are you doing this using
some component which requires a regular expression, or could you split
it into a regular expression and a length check?

Jon
No, not neccessarily. I have thought about your suggestion earlier.

What I am trying to do is build a generic control for different input
formats (strings, numbers with special input formats for each) and I'd
like the validation mechanism to be the same for all types if possible.
Therefore I ventured into regular expressions, which seen appropriate to
me for in this case.

Tobi
Aug 23 '07 #3
Tobias Schröer <to*******************@gmx.dewrote:
Does it *have* to be a regular expression? Are you doing this using
some component which requires a regular expression, or could you split
it into a regular expression and a length check?
No, not neccessarily. I have thought about your suggestion earlier.

What I am trying to do is build a generic control for different input
formats (strings, numbers with special input formats for each) and I'd
like the validation mechanism to be the same for all types if possible.
Therefore I ventured into regular expressions, which seen appropriate to
me for in this case.
I suggest you use a delegate to represent the validation - then it can
be as powerful as the client wants it to be, or as simple. You could
provide a regular expression delegate helper for cases where that's
sufficient.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 23 '07 #4
Jon Skeet [C# MVP] schrieb:
Tobias Schröer <to*******************@gmx.dewrote:
>No, not neccessarily. I have thought about your suggestion earlier.

What I am trying to do is build a generic control for different input
formats (strings, numbers with special input formats for each) and I'd
like the validation mechanism to be the same for all types if possible.
Therefore I ventured into regular expressions, which seen appropriate to
me for in this case.

I suggest you use a delegate to represent the validation - then it can
be as powerful as the client wants it to be, or as simple. You could
provide a regular expression delegate helper for cases where that's
sufficient.
Thanks for your response.

Anyway, I'd be interested to know, if the desired validation could be
done by using a regEx. Something like: "(only digit characters and one
or none decimal separator, preceded and followed by at least one decimal
character) with a max length of n".

Tobi
Aug 24 '07 #5
Hello Tobias,
Jon Skeet [C# MVP] schrieb:
>Tobias Schröer <to*******************@gmx.dewrote:
>>No, not neccessarily. I have thought about your suggestion earlier.

What I am trying to do is build a generic control for different
input formats (strings, numbers with special input formats for each)
and I'd like the validation mechanism to be the same for all types
if possible. Therefore I ventured into regular expressions, which
seen appropriate to me for in this case.
I suggest you use a delegate to represent the validation - then it
can be as powerful as the client wants it to be, or as simple. You
could provide a regular expression delegate helper for cases where
that's sufficient.
Thanks for your response.

Anyway, I'd be interested to know, if the desired validation could be
done by using a regEx. Something like: "(only digit characters and one
or none decimal separator, preceded and followed by at least one
decimal character) with a max length of n".

Tobi
It can, but it isn't pretty:

^(?=.{0,10}$)\d+([,.]\d+)?$

It's using a forward look ahead to test the length and your normal pattern
to test the format. You can turn these around if needed:

^(?=\d+([,.]\d+)?$).{0,10}$

--
Jesse Houwing
jesse.houwing at sogeti.nl
Aug 24 '07 #6

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

Similar topics

17
by: Andrew McLean | last post by:
I have a problem that is suspect isn't unusual and I'm looking to see if there is any code available to help. I've Googled without success. Basically, I have two databases containing lists of...
4
by: Xah Lee | last post by:
20050207 text pattern matching # -*- coding: utf-8 -*- # Python # suppose you want to replace all strings of the form # <img src="some.gif" width="30" height="20"> # to # <img...
8
by: regis | last post by:
Greetings, about scanf matching nonempty sequences using the "%" matches a nonempty sequence of anything except '-' "%" matches a nonempty sequence of anything except ']" matches a nonempty...
8
by: 116Rohan | last post by:
I came across a question in one of the Computing olympiad regarding string pattern matching. Write a program that will accept a fraction of the form N/D, where N is the numerator and D is the...
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
2
by: laura | last post by:
Hi, I'm looking for the algorithm which does the lowest number of comparison for the string matching problem (in the worst case). I know that the complexity is liniar in the length of the...
4
by: J | last post by:
Hello Group- I have limited programming experience, but I'm looking for a generic way to search through a root directory for subdirectories with similar names, organize and group them by...
6
by: andyalean1 | last post by:
Hello, I am trying to match an Id number that is shared across 2 xml files.I seem to be do the right thing bu it won`t display a match.Can you please help me find my error. I have a problem matching...
2
by: Zetten | last post by:
I've got a script which basically goes line by line through an external file, and hands it to the screen in a slightly parsed form. I'd like to apply another bit of parsing to what's coming in...
9
by: warrior2009 | last post by:
I have 2 lists (unlinked and unrelated), where one can be called the parent and the other the child with one to many relationship between parent-child. The child table is really big, over a million...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.