This gets all numbers but not floating numbers
Regex(#"^\d+$")
I need it to get these values as well:
1234.12345545
0
12313
12313.13
-12131
-1313.13211312
For matching all of the above; the most appropriate regex is probably
#"^[+-]?\d+(\.\d+)?$"
This matches all of the above; but not numbers on the format .3456.
It also matches numbers on the format +123 and -1234.5678
Try this here
^(?:[-+]?[1-9]\d*|0)?(?:\.\d+)?$
This will additionally match the empty string.
See it online here on Regexr
If matching the empty string is not wanted, then you can add a length check to your regex like
^(?=.+)(?:[-+]?[1-9]\d*|0)?(?:\.\d+)?$
The positive lookahead (?=.+) ensures that there is at least 1 character
Related
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*)?)*$.
I have a string in c#, say str. Using regex, how can I check if it matches the format 'n, To: n'.
Here n is a numeric value between 0 and 4999.
If the numbers need not be same:
([0-4]?\d{1,3}) To:([0-4]?\d{1,3})
If the numbers need to be the same; your pattern will be:
([0-4]?\d{1,3}) To:\1
I have the following regex: (\d{14}) decimal that matches 14 character long number. The problem is that it also matches numbers, that are 16 characters long. I need to add a condition to match if there are no numbers at beginning or end of string.
So for example 112222222222222233 wouldn't be a match i want, but xx22222222222222xx would be match I need.
use word boundary \b
\b\d{14}\b
M42's answer can work in cases where the number is delimited by spaces or other word delimiters. But if you want to match a number in a word containing non-digits (like your example xx22222222222222xx) something like this should work:
(^|[^\d])\d{14}([^\d]|$)
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...
I am looking for a regular expression that validates only positive numbers.
e.g.
0 is invalid.
0.123 is valid.
any positive integer is valid.
any negative number is invalid
If you are simply trying to validate input numbers as valid positive numbers, you don't need Regex - simply use the Parse or TryParse methods defined on Double or Decimal and check for the value being positive.
decimal test;
if(decimal.TryParse(myString, out test))
{
// parsed OK, myString is a valid decimal
if(test > 0)
{
// yay, it is positive!
}
}
I'm not convinced that a regex is the best way to test for positive numbers, but if you must use a regex for some reason then this should meet your stated requirements:
^(?:[1-9]\d*(?:\.\d+)?|0\.0*[1-9]\d*)$
Try this
^(?!-|0(?:\.0*)?$)\d+(?:\.\d+)?$
See it here on Regexr
^ the start of the string
(?!-|0(?:\.0*)?$) negative lookahead assertion, fails when the string starts with "-" or a 0 and 0.0* followed by the end of the string.
\d+ matches at least one digit
(?:\.\d+)? matches optionally a dot followed by at least one digit
$ the end of the string
this can help u
Positive Number --- ^\d*\.{0,1}\d+$