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

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+)?)\)$

Related

regex for a special pattern of phone number extension

I need a reg-ex for a pattern in a text box where pattern be as follow :
+1245 here first always should be a plus sign (+) and rest of all 4 should be digits (0 to 9)
I have used the other available reg-ex on google but they are mostly for the complete phone number not for extension .
Try this regex:
^[+][0-9]{4}.*$
This will match a leading plus followed by exactly four digits, which can also be followed by anything else.
If your entire string is as described, use this pattern:
^\+\d{4}$
^ means the beginning of the string
\+ means + (escape the + sign)
\d{4} means exactly 4 digits
$ means the end of the string

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})?

Regex to match a doubles separated by one space only

I am trying to check a match as the user enters values in the text box.
Valid values for the textbox are like this:
"-"
"-5.5"
"-5.5 6.5 7.5"
Invalid would be
"-5.5 6.5 "
Edit: ^there is more than one space between -5.5 and 6.5, but it doesn't show for some reason.
"3.5 "
^(-?)(\d+\.?\d?)\s?(-?\d+\.?\d?)
Keep in mind that the negative sign is the only special character, other than the decimal point, allowed in here.
Thanks.
You can use this:
^-(?:\d+[.]\d+(?:[ ]\d+[.]\d+)*)?$
Explanation:
^
- // Match '-'
(?: // An optional non-capturing group
\d+[.]\d+ // Match pattern - 14.45
(?: // A 0 or more times repeating Non-capture group
[ ] // A space
\d+[.]\d+ // Pattern matching - 14.56
)*
)?
$
I think this is what you're looking for:
^(-|-?\d+\.?\d?([ ]-?\d+\.?\d?)*)$
This will match either a single hyphen or any number of space-separated positive or negative numbers with an optional decimal point and at most one digit after it. This will allow values like "5.5 6.5" or "-5.5 -6.5" (your question didn't specify if it should match those or not)
You can test it here.
Update
This will allow many more matches, but satisfies the new requirement of supporting every valid sequence as the user is typing. Of course, it allows even more, since it's impossible to determine the difference between invalid input and input which is merely incomplete (e.g. -5 -).
^(-?(\d+\.?\d?( |$)|$))*$
You can test it here.
You can try this:
^(-(\d+\.\d+)*([ ]\d+\.\d+)*)$

Get a number with exactly x digits from string

im looking for a regex pattern, which matches a number with a length of exactly x (say x is 2-4) and nothing else.
Examples:
"foo.bar 123 456789", "foo.bar 456789 123", " 123", "foo.bar123 " has to match only "123"
So. Only digits, no spaces, letters or other stuff.
How do I have to do it?
EDIT: I want to use the Regex.Matches() function in c# to extract this 2-4 digit number and use it in additional code.
Any pattern followed by a {m,n} allows the pattern to occur m to n times. So in your case \d{m,n} for required values of m and n. If it has to be exactly an integer, use\d{m}
If you want to match 123 in x123y and not in 1234, use \d{3}(?=\D|$)(?<=(\D|^)\d{3})
It has a look ahead to ensure the character following the 3 digits is a non-digitornothing at all and a look behind to ensure that the character before the 3 digits is a non-digit or nothing at all.
You can achieve this with basic RegEx:
\b(\d\d\d)\b or \b(\d{3})\b - for matching a number with exactly 3 digits
If you want variable digits: \b(\d{2,4})\b (explained demo here)
If you want to capture matches next to words: \D(\d{2,4})\D (explained demo here)
\b is a word boundary (does not match anything, it's a zero-match character)
\d matches only digits
\D matches any character that is NOT a digit
() everything in round brackets will capture a match

Categories

Resources