I'm letting users enter GPS values through a form, they all have the same form, some examples:
49.082243,19.302628
48.234142,19.200423
49.002524,19.312578
I want to check the entered value using PHP (using preg_match(), I guess), but as I'm not good in regex expressions (oh, dumb me, I should finally learn it, I know), I don't know how to write the expression.
Obviously it should be:
2x (numbers), 1x (dot), 6x (numbers), 1x (comma), 2x (numbers), 1x (dot), 6x (numbers)
Any suggestions how to write this in regex?
The other answers I see don't take into account that longitude goes from -180 to 180 and latitude goes from -90 to 90.
The proper regex for this would be (assuming the order is "latitude, longitude"):
/^(-?[1-8]?\d(?:\.\d{1,6})?|90(?:\.0{1,6})?),(-?(?:1[0-7]|[1-9])?\d(?:\.\d{1,6})?|180(?:\.0{1,6})?)$/
This regex covers having no less than -90 and no more than 90 for latitude as well as no less than -180 and no more than 180 for longitude while allowing them to put in whole numbers as well as any number of decimal places from 1 to 6, if you want to allow greater precision just change {1,6} to {1,x} where x is the number of decimal place
Also, if you capture on group 1 you get the latitude and a capture on group 2 gets the longitude.
Something like:
/^(-?\d{1,2}\.\d{6}),(-?\d{1,2}\.\d{6})$/
^ anchors at the start of input
-? allows for, but does not require, a negative sign
\d{1,2} requires 1 or 2 decimal digits
\. requires a decimal point
\d{6} requires exactly 6 decimal digits
, matches a single comma
(repeat the first 5 bullets)
$ anchors at the end of input
I have included capturing parentheses to allow you to extract the individual coordinates. Feel free to omit them if you don't need that.
All-around useful regex reference: http://www.regular-expressions.info/reference.html
/$-?\d{2}\.\d{6},-?\d{2}\.\d{6}^/
Expanding on the other answer:
/^-?\d\d?\.\d+,-?\d\d?\.\d+$/
Based on your example, this will do it:
if (preg_match('/(-?[\d]{2}\.[\d]{6},?){2}/', $coords)) {
# Successful match
} else {
# Match attempt failed
}
Explanation:
( # Match the regular expression below and capture its match into backreference number 1
- # Match the character “-” literally
? # Between zero and one times, as many times as possible, giving back as needed (greedy)
[\d] # Match a single digit 0..9
{2} # Exactly 2 times
\. # Match the character “.” literally
[\d] # Match a single digit 0..9
{6} # Exactly 6 times
, # Match the character “,” literally
? # Between zero and one times, as many times as possible, giving back as needed (greedy)
){2} # Exactly 2 times
Related
^\$?(\d{1,3},?(\d{3},?)*\d{3}(\.\d{0,2})?|\d{1,3}(\.\d{0,2})?|\.\d{1,2}?)$
I actually found this to help me to validate the amount of $. The problem is that I want to have a limited amount to validate between 0$ and 99.99$. also amounts like 01.20 and 10.1 are not acceptable but 1.20$ 10.10 are.
Is there something I could modify on this regex.
Also this is for the use of my php code. I know I need to put one more backlash on the regex to make it work on php. thanks.
See regex in use here
^(?:\d{1,2}(?:\.\d{2})?|\.\d{2})\$$
^ Assert position at the start of the line
(?:\d{1,2}(?:\.\d{2})?|\.\d{2}) Match either of the following options
\d{1,2}(?:\.\d{2})? Option 1
\d{1,2} Match a digit one or two times
(?:\.\d{2})? Optionally match a decimal point followed by exactly two digits
\.\d{2} Option 2. Match a decimal point followed by exactly two digits
\$ Match $ literally
$ Assert position at the end of the line
Here is my suggestion:
^(?:0|[1-9]\d{0,2})(?:,?\d{3})*(?:\.\d{2})?\$$
^ asserts position at start of a line
(?:0|[1-9]\d{0,2}) matches either a 0 or a non-zero digit once followed by an optional digit
(?:,?\d{3})* matches an optional thousands separator followed by three digits zero or more times
(?:\.\d{2})? optionally matches a decimal place followed by two digits
\$ literally matches the dollar sign symbol
$ asserts position at the end of a line
Fiddle: Live Demo
I have been testing and googling but still could'nt work out pattern to validate comma separated numbers.
9 digits long, numbers only, no spaces, leading number for each digit cannot be zero
Tried
^(?:\s*\d{9}\s*(?:,|$))+$
but no go
Note that commas are required since the input file should between 3 up to 20 (max) comma separated integers
You mentioned that there should be no spaces, but you used \s* (0+ whitespaces) in your pattern. Also, (?:,|$) matches a , or end of string, so your pattern allows a trailing ,.
I suggest using
^[1-9]\d{8}(?:,[1-9]\d{8}){2,19}$
See the regex demo
Details:
^ - start of string
[1-9] - the first digit of the 9-digit number cannot be zero, 1 to 9 only
\d{8} - the 8 remaining digits of the number
(?:,[1-9]\d{8}){2,19} - 2 to 19 (in total, 3 to 20) occurrences of
, - comma
[1-9]\d{8}){2,19} - see above
$ - end of string.
preg_match('=^[1-9][0-9,]*$=', $x); should do it, unless you're saying the number must be 9 digits long (with optional commas? or are the commas required?), in which case try something like preg_match('=^[1-9][0-9]{2},?[0-9]{3},?[0-9]{3}$=', $x);
How can I match numbers between 900 and 950?.
I have the following regex to match numbers 900-950 :
/9[0-5][0-9]{3}/
But it is also matching 955 in a string,
How can I fix it to match untill 900 to 950 only?
To match 900-950 number range, use
^9(?:[0-4][0-9]|50)$
Or, if it is inside a larger text
(?<!\d)9(?:[0-4][0-9]|50)(?!\d)
See this regex demo
The ^ is the start of string anchor, and $ is the end of string anchor.
The (?<!\d) is a negative lookbehind making sure the preceding symbol is not a digit. The (?!\d) is a negative lookahead that makes sure the next character is not a digit.
Using a non-capturing group (?:...) we avoid capturing what we do not need.
One more option: if the number is in-between non-word characters, you can leverage word boundaries:
\b9(?:[0-4][0-9]|50)\b
Note that I am using 9(?:[0-4][0-9]|50), not (?:9[0-4][0-9]|950). Although the second one is more readable, it is less effecient from the performance point of view since 9(?:[0-4][0-9]|50) involves less backtracking (i.e. it fails quicker if there is no match).
split it up into ORs:
/(9[0-4][0-9]|950)/
9[0-4][0-9] will match from 900 to 949
950 covers your 950 case without going over
if you want to make sure NOT to match anything else (like you might not want to match 1950 and might only want to match 950 on its own):
/(?<![0-9])(9[0-4][0-9]|950)(?![0-9])/
(?<![0-9]) means don't allow a number before and (?![0-9]) means don't allow a number after (negative look behind and ahead)
I think the easiest would be this:
/[^\d]9([0-4][0-9])|50[^\d]/
My question is how do I modify the following regex to get the numbers from this string 160bhp, BUT also the numbers from the 90bhp, string?
For now, it takes only the 3 numbers and ignores the 2. I am trying with | without luck.
preg_match_all('/(\d{3})bhp\b,/', $str2b, $bhps);
Am guessing you may want to generalise this to match one or more digits before the bhp (with no upper limit)? If that's the case, use this:
preg_match_all('/(\d+)bhp\b,/', $str2b, $bhps);
Taking this a bit further, would zero or more whitespace characters be allowed between, e.g. "200 bhp"? If so, use this:
preg_match_all('/(\d+)\s*bhp\b,/', $str2b, $bhps);
And finally, does it need to be case insensitive, e.g. to allow 300BHP? If so, put an i after the final slash:
preg_match_all('/(\d+)\s*bhp\b,/i', $str2b, $bhps);
You simply change the value between the brackets to be {2,3}.
preg_match_all('/(\d{2,3})bhp\b,/', $str2b, $bhps);
This basically provides a minimum/maximum number of the preceding character definition that is to be matched.
\d{3} is saying there needs to be exactly 3 digits. Changing the {3} to * should solve the problem and allow you to retrieve any number of consecutive digits before the bhp.
preg_match_all('/(\d*)bhp\b,/', $str2b, $bhps);
regexpal is an excellent online regex tester.
preg_match_all('/(\d{2,3})bhp\b,/', $str2b, $bhps);
There's an additional quantifier that allows you to specify how many times a token can be repeated. The syntax is {min,max}, where min is zero or a positive integer number indicating the minimum number of matches, and max is an integer equal to or greater than min indicating the maximum number of matches. If the comma is present but max is omitted, the maximum number of matches is infinite.
Source: http://www.regular-expressions.info/repeat.html
Bt if you want it to match also 1bhp and 160000bhp for example, use this instead:
preg_match_all('/(\d+)bhp\b,/', $str2b, $bhps);
Which will capture any number of digits on the left of bhp
I want to write php regular expression to find uppercase string , which can also contain one number and spaces, from text.
For example from this text "some text to contain EXAM PL E 7STRING uppercase word" I want to get string- EXAM PL E 7STRING ,
found string should start and end only with uppercase, but in the middle, without uppercase letters can also contain(but not necessarily ) one number and spaces. So, regex should match any of these patterns
1) EXAMPLESTRING - just uppercase string
2) EXAMP4LESTRING - with number
3) EXAMPLES TRING - with space
4) EXAM PL E STRING - with more than one spaces
5) EXAMP LE4STRING - with number and space
6) EXAMP LE 4ST RI NG - with number and spaces
and with total length string should be equal or more than 4 letters
I wrote this regex '/[A-Z]{1,}([A-Z\s]{2,}|\d?)[A-Z]{1,}/', that can find first 4 patterns, but I can not figure it out to match also the last 2 patterns.
Thanks
There is a neat trick called a lookahead. It just checks what is following after the current position. That can be used to check for multiple conditions:
'/(?<![A-Z])(?=(?:[A-Z][\s\d]*){3}[A-Z])(?!(?:[A-Z\s]*\d){2})[A-Z][A-Z\s\d]*[A-Z]/'
The first lookaround is actually a lookbehind and checks that there is no previous uppercase letter. This is just a little speedup for strings that would fail the match anyway. The second lookaround (a lookahead) checks that there are at least four letters. The third one checks that there are no two digits. The rest just matches then a string of the allowed characters, starting and ending with an uppercase letter.
Note that in the case of two digits this will not match at all (instead of matching everything up to the second digit). If you do want to match in such a case, you could incorporate the "1 digit" rule into the actual match instead:
'/(?<![A-Z])(?=(?:[A-Z][\s\d]*){3}[A-Z])[A-Z][A-Z\s]*\d?[A-Z\s]*[A-Z]/'
EDIT:
As Ωmega pointed out, this will cause problems if there are less then four letters before the second digit, but more after that. This is actually quite tough, because the assertion needs to be, that there are more than 4 letters before the second digit. Since we do not know where the first digit occurs in those four letters, we have to check for all possible positions. For this I would do away with the lookaheads altogether, and simply provide the three different alternatives. (I will keep the lookbehind as an optimization for non-matching parts.)
'/(?<![A-Z])[A-Z]\s*(?:\d\s*[A-Z]\s*[A-Z]|[A-Z]\s*\d\s*[A-Z]|[A-Z]\s*[A-Z][A-Z\s]*\d?)[A-Z\s]*[A-Z]/'
Or here with added comments:
'/
(?<! # negative lookbehind
[A-Z] # current position is not preceded by a letter
) # end of lookbehind
[A-Z] # match has to start with uppercase letter
\s* # optional spaces after first letter
(?: # subpattern for possible digit positions
\d\s*[A-Z]\s*[A-Z]
# digit comes after first letter, we need two more letters before last one
| # OR
[A-Z]\s*\d\s*[A-Z]
# digit comes after second letter, we need one more letter before last one
| # OR
[A-Z]\s*[A-Z][A-Z\s]*\d?
# digit comes after third letter, or later, or not at all
) # end of subpattern for possible digit positions
[A-Z\s]* # arbitrary amount of further letters and whitespace
[A-Z] # match has to end with uppercase letter
/x'
That gives the same result on Ωmega's lengthy test input.
I suggest to use regex pattern
[A-Z][ ]*(\d)?(?(1)(?:[ ]*[A-Z]){3,}|[A-Z][ ]*(\d)?(?(2)(?:[ ]*[A-Z]){2,}|[A-Z][ ]*(\d)?(?(3)(?:[ ]*[A-Z]){2,}|[A-Z][ ]*(?:\d|(?:[ ]*[A-Z])+[ ]*\d?))))(?:[ ]*[A-Z])*
(see this demo).
[A-Z][ ]*(?:\d(?:[ ]*[A-Z]){2}|[A-Z][ ]*\d[ ]*[A-Z]|(?:[A-Z][ ]*){2,}\d?)[A-Z ]*[A-Z]
(see this demo)