Regular expression to allow numbers between -90.0 and +90.0 - c#

What is the regular expression to allow for numbers between -90.0 and +90.0? The numbers in between can be floating or whole numbers.

I don't think you want to use a Regex for this. Use Double.Parse() (or Double.TryParse()) if your data is stored in a string, and then check the resulting value to ensure that it falls within the desired range. For example:
public bool IsInRange(string value)
{
bool isInRange = false;
double parsed = 0;
if (Double.TryParse(value, out parsed))
{
// use >= and <= if you want the range to be from -90.0 to 90.0 inclusive
isInRange = value > -90.0 && value < 90.0;
}
return isInRange;
}
If your value is already a double, then it's even easier -- no parsing required.

Not that you really want to use a Regex here (you should parse it, instead, and do the comparison on a numeric type - such as float, or double). But, you could do this:
-?(\d|([1-8][0-9])(\.\d)?)|(90(\.0)?)
This will match -90.0 to 90.0, inclusive. If you want it to be exclusive, drop the 90.0 clause.
negative (optional):
-?
single digit
OR double digit, 10-89
\d|([1-8][0-9])
PLUS decimal, 0-9 (optional):
(\.\d)?
OR 90
90
PLUS decimal, 0 (optional):
(\.0)?
If you want to support more decimal points, then change the 0-89.9 clause to:
Specific precision (seven, in this case) \d|([1-8][0-9])(\.\d{1,7})?
Infinite precision \d|([1-8][0-9])(\.\d+)?
Escape, if necessary

"Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."
This is a problem that would be better solved with a check. But, if you want a regex, you can have a regex.
-?0*((90(\.0*)?)|([1-8]?\d(\.\d*)?))
will work, I think. Match an optional '-', followed by any number of zeros, followed by either 90 with any number of zeros, or a number that consists of an optional tens digit between 1 and 8, followed by a ones digit, followed by a optional decimal and decimal places. But you see why using a regex for this is so messy. Check the bounds as a numbers, not a series of numerals.

A straightforward regex for +/- option sign of a 90.0 range with optional float of 1 decimal place would be :
[-+]?90(?:\.0?)?|[-+]?(?:\d|[1-8]\d)(?:\.\d?)?
Expanded
# [-+] 90.0
[-+]? 90
(?: \. 0? )?
|
# [-+] 89.9 to 0.0
# 0.0 to 89.9
[-+]?
(?:
\d
| [1-8] \d
)
(?: \. \d? )?

Related

Decimal and only positive numeric values

What regex i can use for positive numeric and decimal values ?
Accepted values: 0, 22.5 , 0.35 , 89 , .38
Invalid values : -22 , -.25 , -0.66 , abc, -22abc, 55er
I have tried this but i get error
^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$"
Too many )'s
^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$
You're missing a closing ) in the regex.
^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*)$
However, I'd strongly suggest you look at Decimal.TryParse instead of trying to validate numbers using the format of a string yourself.
I would simply your pattern to this:
\d*(?:\.\d+)?
Demo
This seems to cover all your positive decimal number use cases. The decimal component is optional, meaning that the pattern covers integers. Decimal numbers are covered, and also numbers which only have a decimal component.
Note that you might have to surround the pattern with ^$ anchors, depending on the particular API you are using.
Edit:
If you also need to ensure that empty string is not matched, then we can add a lookahead to the above pattern:
(?=.)\d*(?:\.\d+)?
Demo
Or, if you want to stay away from Regex, one of these might work:
private static bool IsPositiveDecimal(string decimalString)
{
return decimal.TryParse(decimalString, out var aDecimal) && aDecimal >= 0;
}
or
private static bool TryParsePositiveDecimal(string decimalString, out decimal aDecimal)
{
return decimal.TryParse(decimalString, out aDecimal) && aDecimal >= 0;
}
If you feed the latter one a non-positive decimal, it will return false, but will return the parsed decimal in the out parameter.
This should suit your needs without injecting a complicated Regex into your code.
I believe this regex should meet all the cases and will only match valid samples and discard all invalid ones.
^(?:[1-9]\d*(\.\d+)?|0?\.\d+|0)$
Regex Explanation:
^ - Start of string
(?: - Start of non capture group
[1-9]\d* - Matches a number without unneeded preceding zeroes. Will invalidated 01 or 001
(\.\d+)? - Can optionally have decimal part
| - Alternation
0?\.\d+ - Allows only decimal numbers that can optionally have zero before them. E.g. to support numbers like 0.11 or .11
| - Alternation
0 - Supporting literal zero as a special case
) - closing of non-capture group
$ - End of string
Demo
^\d*\.?\d+$
There will also match 0.00 .00 0.0

before point 3 numbers should accept 0-9 after point decimal 4 digits should accept

I need to validate the text box server side by using regular exp which should accept at most 3 digits [0-9] before the decimal point and 4 digits [0-9] after the decimal point. The part after the decimal point should be restricted to exactly 4 digits (not more or less)
Correct examples:
32.4240
10.0240
100.6400
2.0260
43.0000
The "#" custom format specifier serves as a digit-placeholder symbol. If the value that is being formatted has a digit in the position where the "#" symbol appears in the format string, that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string.
value.ToString("###.####");
If you want to use regexp then try ^\d{1,3}(?:\.\d{1,4})$

Match float number at the beginning of a text -Regular Expression

I want to match these examples flowing a format as #.###,##
Valid examples
455,80SomeText
1,00
30,82
7,78 SomeText
622,21
8.542,85
Invalid examples
455,482
54,1
7454,50
I have tried this: ^[0-9]+(\,[0-9][0-9])
Update 1
Number format #.###,##
Could contain some text after the number
You aren't accounting for the thousands separator at all in your regex...
^[0-9]{0,3}(?:\.[0-9]{3})*,[0-9]{2}(?![0-9])
regex101 demo
If you don't want to accept ,42 either, use:
^[0-9]{1,3}(?:\.[0-9]{3})*,[0-9]{2}(?![0-9])
regex101 demo
(?:\.[0-9]{3})* allows for the thousands.
The comma doesn't need to be escaped, and (?![0-9]) (a negative lookahead) is to prevent the number from being followed by more numbers.
Try this regular expression:
^\-?\d{1,3}(\.\d\d\d)*(,\d+)?
Broken out:
^ # drop anchor at the start of the line. Then...
\-? # match an optional negative sign, followed by...
\d{1,3} # match 1-3 decimal digits, followed by...
( # a group, consisting of
\. # * a thousands separator, followed by
\d\d\d # * 3 decimal digits
)* # with the group repeated zero or more times, followed by...
( # a group, consisting of
, # * a decimal point, followed by
\d+ # * 1 or more decimal digits
)? # with the group being optional
You should note that the thousands separator and decimal point are culture-specific. Further, not all cultures clump digits in groups of 3.
To make this portable across cultures, you'll need to instantiate a suitable System.Globalization.CultureInfo, drill down to its NumberFormatInfo property and build the regular expression on the fly using the culture's rules for how numbers are composed.
I am not exactly sure what format you are looking for, but it sounds like you could use the repeat qualifiers in RegEx like ^[0-9]+(,[0-9]{2}) I think the problem in your expression is you are escaping the comma which is not a regex special character.
Here is a good reference on RegEx: http://www.regular-expressions.info/repeat.html

Regular Expression for decimal numbers (at max 3 decimal numbers with comma)

How can I write a regular expression that validates an input text box that should contain only decimal values? The value can have at max 3 decimals (but also none) with comma as the separator.
For example, these values given below are valid:-
1,234
1,23
1,2
1
These are not valid:
1,2345 (too many decimal numbers)
A (a letter is not a number)
(a space or string empty)
1.234 (used a dot instead of a comma)
Try something like this:
\d+(?:,\d{1,3})?
Explained:-
\d+ # multiple digits
(?: # start non-capturing group
, # a comma
\d{1,3} # 1-3 digits
)? # end non-capturing group, made optional
You could use a pattern like this:
[0-9]+(,[0-9]{1,3})?
How about #"\d+,?\d{0,3}": 1 or more digits, then an optional comma, then 0 to 3 more digits. This assumes that you allow any number of digits before the comma. In your examples you only have one, in which case you would want to remove the +.
If the value 1, is not valid, you'll have to move the ? to the end: #"\d+(,\d{1,3})?"
Note if you only want one character before the decimal, remove the +
^\d+(,(\d?){3})?$
^ //start
\d+ //one or more decimal digits
(,(\d?){3})? //a comma, followed by up to 3 decimal digits, optionally
$ //end
If you don't want 1, to be accepted, then the middle section can be (,\d(\d?){2})?

Regular expression needed for this pattern: point(latitude,longitude)

I am trying to validate text that follows the pattern below:
Must have the text "point(" at the beginning
Must follow it by a Latitude numerical value with up to 5 decimal places (example: 42.12345)
Must follow it by a comma ","
Must follow it by a Longitude numerical value with up to 5 decimal places (example: -81.12345)
Must follow it by a closing parentheses ")"
Matching example:
point(42.12345,-81.12345)
Any help is greatly appreciated.
Thanks.
You can easily build your regex with a little bit of break-up here.
To match point( at the beginning, use - ^point\(
To match a latitude or longitude numbers, use - [-]?\d+(?:\.\d+)?
And again, to match ) at the end, use \)$.
For [-]?\d+(?:\.\d+)?, here's an explanation: -
[-]? - matches an optional negative (-) sign at the starting (? quantifier at the end means 0 or 1)
\d+ - matches one or more digits
(?:\.\d+)? - matches an optional decimal, followed by one or more
digits. dot(.) is a special meta-character in Regex, so you need to escape it, if you want to match it.
Also, to limit your number of digits to 5, you can use - \d{1,5} instead of \d+, which matches minimum 1 and maximum 5 digits.
^(caret) and $(dollar) anchors matches the beginning and end of the string.
So, here's your regex: -
^point\([-]?\d+(?:\.\d{1,5})?,[-]?\d+(?:\.\d{1,5})?\)$
Try this:
^point\(\-?\d+\.\d{1,5},\-?\d+\.\d{1,5}\)$
Must have the text "point(" at the beginning: ^point\(
Must follow it by a Latitude numerical value with up to 5 decimal places (example: 42.12345): \-?\d+\.\d{1,5}
Must follow it by a comma ",": ,
Must follow it by a Longitude numerical value with up to 5 decimal places (example: -81.12345): \-?\d+\.\d{1,5}
Must follow it by a closing parentheses ")": \)$
The latitude and longitude logic can be further broken up like this.
\-? = match on a negative sign if it exists (must escape with \ because - has special meaning in RegEx)
\d+ = match one or more decimal characters (i.e. 0 through 9)
\. = match the period (. alone has special meaning, must escape it)
\d{1,5} = match one to five decimal characters
Something like this:
point\((?<lat>-?\d+\.\d{1,5}),(?<long>-?\d+\.\d{1,5})\)
Try using a regex tool, like expresso: http://www.ultrapico.com/Expresso.htm
how about:
^point\((\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)\)$

Categories

Resources