Regular expression in C# for Number with Decimal Point & Commas - c#

I need to enter amount in a textbox which allows numbers with decimal point and commas.
What is the regular expression for this?
I used the below
txtInitialAmountGuarDetails.ValidationSettings.RegularExpression.ValidationExpression
= #"^[-+]?\d*[0-9](|.\d*[0-9])(|,\d*[0-9])?$";
But it not working for large numbers like 300,000,000,000,000.

Build it up piecemeal. Given a US locale, a number with these rules has in order:
The string beginning: ^
An optional sign: [+-]?
Up to 3 digits: \d{1,3}
A comma followed by 3 digits, repeated any number of times: (?:,\d{3})*
An optional decimal point and decimal part: (?:[.]\d+)?
The string end: $
Do you have restrictions on the number of digits after the decimal point? Then change the last plus sign to {2} for 2 digits.
So, the regex is:
#"^[+-]?\d{1,3}(?:,\d{3})*(?:[.]\d+)?$"
Or, if you want to explain your work, use the x option and:
#"(?x) # Extended format.
^[+-]? # Optional sign.
\d{1,3} # Initial 1-3 digits.
(?:,\d{3})* # Any number of commas followed by 3 digits.
(?:[.]\d+)?$" # An optional decimal point followed by any number of digits.
But does C# have a locale-dependent validator already?

I have not run it, but you can try it out.
var regexp =/^\s*?([\d\,]+(\.\d{1,2})?|\.\d{1,2})\s*$/;

This works: \d{1,3}(,\d{3})*\.{0,1}(\d{3},)*\d{0,3}
As for the after the comma issue, any choice should be fine. If you go with commas, my regex works. If you do 5 digits then a space just replace the end with (\d{5}\s{1})*\d{0,5}. And ofcourse if you just dont use any deliminator after the decimal you just put \d*

You can try this regex too:
^([+-]?\d{1,3}(?:,\d{1,3})*(?:\.\d+)*)$
Keep in mind . has a specific meaning in regex engine so it is necessary to escape it.
I would also suggest you to not use regex for this task instead look at masked textbox.

try this one:
^([0-9]{3}[,.]|)+[0-9]{0,3}$
let me know if it needs any enhancements...

Related

Repeating pattern matching with Regex

I am trying to validate an input with a regular expression. Up until now all my tests fail and as my experience with regex is limited I thought someone might be able to help me out.
Pattern: digit (possibly "," digit) (possibly ;)
A String may not begin with a ; and not end with a ;.
Digits are allowed to stand alone or with
My regEx (not working): ((\d)(,\d)?)(;?) the problem is it does not seem to check until the end of the string. Also the optional parts are giving me headaches.
Update: ^[0-9]+(,[0-9])?(;[0-9]+(,[0-9])?)+$this seems to work better but it does not match the single digit.
OK:
2,3;4,4;3,2
2,3
2
2,3;3;4,3
NOK:
2,3,,,,
2,3asfafafa
;2,3
2,3;;3,4
2,3;3,4;
Your ^[0-9]+(,[0-9])?(;[0-9]+(,[0-9])?)+$ regex matches 1 or more digits, then an optional sequence of , and 1 digit, followed with one or more similar sequences.
You need to match zero or more comma-separated numbers:
^\d+(?:,\d+)?(?:;\d+(?:,\d+)?)*$
^
See the regex demo
Now, tweaking part:
If only single-digit numbers should be matched, use ^\d(?:,\d)?(?:;\d(?:,\d)?)*$
If the comma-separated number pairs can have the second element empty, add ? after each ,\d (if single digit numbers are to be matched) or * (if the numbers can have more than one digit): ^\d(?:,\d?)?(?:;\d(?:,\d?)?)*$ or ^\d+(?:,\d*)?(?:;\d+(?:,\d*)?)*$.

Regex validator not working as expected

Updated
I need to validate numbers, with decimal separator as , (comma) but this is not working:
validate.ValidationExpression = #"^[1-9]\d*(,\d+)?$";
123,01; 0,01; 0,001; 123456789,01 these are the numbers I would like to allow
Any help will be appreciated.
How about this:
^\d+(,\d+)?$
Visual explanation:
http://www.regexper.com/#%5E%5Cd%2B(%2C%5Cd%2B)%3F%24
That will:
^ Start at the beginning of a line
\d+ Find at least one, but as many as possible digits
( Start of a group
, The decimal sign
\d+ Find at least one, but as many as possible digits
) End of the group
? Makes the entire group optional
$ End of the line
This should capture integers and decimal numbers alike.
Try it out here:
http://regexr.com/398q6
That should do the work
^[-+]?[0-9]*,?[0-9]+$
It validates all of your examples and it supports negative numbers and numbers without any floating part.
If you want all your numbers to be accepted (123,01; 0,01; 0,001; 123456789,01) you have to change the first number 1 to 0, like this:
validate.ValidationExpression = #"^[0-9]\d*(,\d+)?$";
since you are basicly saying that any number 1-9 followed by , and 0-9 are fine so it wont select the ones that has a 0 before the ,.
validate.ValidationExpression = #"^\d+[,]?\d*$";
this will accept both 90 and 90,000

Regex pattern allowing alphabets, numbers, decimals, spaces and underscore

I am not good with regular expression patterns.
I have to put a validation on an string, to allow
only alphabets, numbers, decimals, spaces, comma and underscore
for allowing the alphabets and spaces I have /^[a-zA-Z][a-zA-Z\\s]+$/
Please help me in creating all the above conditions in one pattern.
Thanks
this regex should work for your requirements
'[a-zA-Z0-9_. ,]*'
In the regex, I specified the range a to z, A to Z (uppercase), 0 to 9 and the single character _, decimal point ".", space and a comma.
If you want to make sure you want at least one character after the first letter, you can replace the * with a +, or {2,} with at least 2 more characters, or {2,5} with between 2 and 5 characters.
You can try:
/^[\w., ]+$/
I don't know what are the requirements for the starting char, if there are any.
Rahul's answer gave me the direction to think, but for the visitors, may be this too can be helpfull
patternForClasName = /^([a-zA-Z0-9 _]+\.)*[a-zA-Z0-9 _]+$/;
// Allowing valid className which has a format abcsasa.dsd.dsd(the class or a package name can have an underscore or a numerical)
patternForName = /^([a-zA-Z0-9 _-]+)$/;
// Allowing alphanumeric + spaces + (_)underscore and a (-)dash
patternForDescription = /^([a-zA-Z0-9 _-]+[\,\.]+)*[a-zA-Z0-9 _-]*$/;
// Allowing alphanumeric,spaces, (_)underscore, (-)dash, comma, decimal
patternURLFormat = /(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/;
// For a valid URL

Discount mask with regex

Is it possible to create a 'dynamic' discount mask that takes % or numbers as discount values? What is the simple way to do this?
the samples of valide input: -25% or 0.25 or -5$ not 0 and two digit after dot
Try
#"(\+|-)?(\d+(\.\d*)?|\.\d+)%?"
It will find:
123.23
12.4%
.34
.34%
45.
45.%
8
7%
34
34%
+2.55%
-1.75%
UPDATE
and with ...
#"(\+|-)?(\d+(,\d{3})*(?!\d)(\.\d*)?|\.\d+)%?"
... you can include thousands separators as well.
I must confess that my second regex expression looks like a cat had walked accross my keyboard. Here the explanation
(\+|-)? optionally ? a plus or a minus sign.
\d+(,\d{3})*(?!\d)(\.\d*)? one or more digits \d+ followed by any number of thousands separators plus three digits (,\d{3})*, not followed by any digit (?!\d) in order to disallow four digits in sequence, optionally followed by a decimal point and any number of digits (\.\d*)?.
|\.\d+ or alternatively a decimal point followed by at least one digit.
%? finally an optional percent sign.
If I understand your question right, you want something like this:
#"^[+-]?(?:\d*\.)?\d+[%$]?$"
That's partly based on your example of -5$. Usually, though, the $ would go in front, so you'd want something like:
#"^(?:\$(?!.*%))?[+-]?(?:\d*\.)?\d+%?$"
That would allow $-5.00, 10, or +20%, but block $5%.
Edit:
Running with Olivier's idea of allowing commas:
#"^(\$(?!.*%))?[+-]?(\d{1,3}((,\d{3})*|\d*))?(\.\d+)?\b%?$"
Expanded to make it easier to understand:
#"^ #Require matching from the beginning of the line
(\$(?!.*%))? #Optionally allow a $ here, but only if there's no % later on.
[+-]? #Optionally allow + or - at the beginning
(
\d{1,3} #Covers the first three numerals
((,\d{3})*|\d*) #Allow numbers in 1,234,567 format, or simply a long string of numerals with no commas
)? #Allow for a decimal with no leading digits
(\.\d+)? #Optionally allow a period, but only with numerals behind it
\b #Word break (a sneaky way to require at least one numeral before this position, thus preventing an empty string)
%? #Optionally allow %
$" #End of line

need a regex for a positive max 2 digits after . floating number

Hi
I need a C# regex for a positive floatin no with maximum 2 digits for decimals. Also the regex should check for letters and alphanumerical chars (not allow them) and not allow also the input value to be empty (0 characters).
Thanks
^[+]?[0-9]+([.][0-9]{1,2})?$
This will force it to have either a + or nothing at the start, followed by at least 1 number, then optional (decimal followed by 1 or 2 numbers)
For others, yes, I know of \d, :digit:, using \., etc. I just prefer using [0-9] and [.], it makes them stand out easier for me.

Categories