I'm letting users enter GPS values through a form, they all have the same form, some examples:
我让用户通过表单输入GPS值,它们都具有相同的形式,例如:
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.
我想用PHP检查输入的值(我猜是使用preg_match()),但因为我在正则表达式中不好(哦,笨我,我应该最后学习它,我知道),我不知道如何写表达式。
Obviously it should be:
2x (numbers), 1x (dot), 6x (numbers), 1x (comma), 2x (numbers), 1x (dot), 6x (numbers)
显然它应该是:2x(数字),1x(点),6x(数字),1x(逗号),2x(数字),1x(点),6x(数字)
Any suggestions how to write this in regex?
有关如何在正则表达式中写这个的任何建议吗?
5 个解决方案
#1
10
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 - \ d {1,2}需要1或2个十进制数字
-
\.
requires a decimal point - \。需要一个小数点
-
\d{6}
requires exactly 6 decimal digits - \ d {6}正好需要6位小数
-
,
matches a single comma - ,匹配一个逗号
- (repeat the first 5 bullets)
- (重复前5个子弹)
-
$
anchors at the end of input - 输入结束时的$ anchors
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
全面有用的正则表达式参考:http://www.regular-expressions.info/reference.html
#2
13
The other answers I see don't take into account that longitude goes from -180 to 180 and latitude goes from -90 to 90.
我看到的其他答案没有考虑到经度从-180到180,纬度从-90到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
这个正则表达式包括纬度不小于-90且不大于90,经度不小于-180且不大于180,同时允许它们输入整数以及从1到1的任意小数位数6,如果你想允许更高的精度,只需将{1,6}更改为{1,x},其中x是小数位数
Also, if you capture on group 1 you get the latitude and a capture on group 2 gets the longitude.
此外,如果您在组1上捕获,则获得纬度,组2上的捕获获得经度。
#3
1
/$-?\d{2}\.\d{6},-?\d{2}\.\d{6}^/
#4
1
Expanding on the other answer:
扩展其他答案:
/^-?\d\d?\.\d+,-?\d\d?\.\d+$/
#5
0
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
#1
10
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 - \ d {1,2}需要1或2个十进制数字
-
\.
requires a decimal point - \。需要一个小数点
-
\d{6}
requires exactly 6 decimal digits - \ d {6}正好需要6位小数
-
,
matches a single comma - ,匹配一个逗号
- (repeat the first 5 bullets)
- (重复前5个子弹)
-
$
anchors at the end of input - 输入结束时的$ anchors
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
全面有用的正则表达式参考:http://www.regular-expressions.info/reference.html
#2
13
The other answers I see don't take into account that longitude goes from -180 to 180 and latitude goes from -90 to 90.
我看到的其他答案没有考虑到经度从-180到180,纬度从-90到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
这个正则表达式包括纬度不小于-90且不大于90,经度不小于-180且不大于180,同时允许它们输入整数以及从1到1的任意小数位数6,如果你想允许更高的精度,只需将{1,6}更改为{1,x},其中x是小数位数
Also, if you capture on group 1 you get the latitude and a capture on group 2 gets the longitude.
此外,如果您在组1上捕获,则获得纬度,组2上的捕获获得经度。
#3
1
/$-?\d{2}\.\d{6},-?\d{2}\.\d{6}^/
#4
1
Expanding on the other answer:
扩展其他答案:
/^-?\d\d?\.\d+,-?\d\d?\.\d+$/
#5
0
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