vieyahn2017 / shellv

shell command test and study
4 stars 1 forks source link

8.22 经纬度正则表达式 #70

Closed vieyahn2017 closed 3 years ago

vieyahn2017 commented 4 years ago

经纬度正则表达式

vieyahn2017 commented 4 years ago

帖子一:

地图经纬度的正则表达

最近因为工作关系需要写两个关于地图经纬度的正则表达式,与大家分享

经度: -180.0~+180.0(整数部分为0~180,必须输入1到5位小数)

正则表达式:

/^[\-\+]?(0?\d{1,2}\.\d{1,5}|1[0-7]?\d{1}\.\d{1,5}|180\.0{1,5})$/

纬度: -90.0~+90.0(整数部分为0~90,必须输入1到5位小数)

正则表达式:

 /^[\-\+]?([0-8]?\d{1}\.\d{1,5}|90\.0{1,5})$/
vieyahn2017 commented 4 years ago

帖子二:

经纬度正则及java代码测试

经度校验:

/^[\-\+]?(0?\d{1,2}|0?\d{1,2}\.\d{1,15}|1[0-7]?\d{1}|1[0-7]?\d{1}\.\d{1,15}|180|180\.0{1,15})$/

纬度校验:

/^[\-\+]?([0-8]?\d{1}|[0-8]?\d{1}\.\d{1,15}|90|90\.0{1,15})$/

Java代码:

String lonMatch = "[\\-+]?(0?\\d{1,2}|0?\\d{1,2}\\.\\d{1,15}|1[0-7]?\\d|1[0-7]?\\d\\.\\d{1,15}|180|180\\.0{1,15})";
String latMatch = "[\\-+]?([0-8]?\\d|[0-8]?\\d\\.\\d{1,15}|90|90\\.0{1,15})";

if (!Pattern.matches(latMatch, lat)){
    target.remove("location");
}
if (!Pattern.matches(lonMatch, lon)){
    target.remove("location");
}
vieyahn2017 commented 4 years ago

8.22 参考帖子一,vba的正则测试代码

Private Function g_tool_isLongitudeFormat(lxxxtude) As Boolean
    '判断经度是否合法  -180 - +180  1-6位小数数字
    g_tool_isLongitudeFormat = True

    If Trim(lxxxtude) = "" Then
        g_tool_isLongitudeFormat = False   '''排空
        Exit Function
    End If

    Set lngFormat = CreateObject("VBscript.regexp")
    lngFormat.Pattern = "^[\-\+]?(0?\d{1,2}\.\d{1,6}|1[0-7]?\d{1}\.\d{1,6}|180\.0{1,6}$"
    lngFormat.IgnoreCase = True

    Set regRes = lngFormat.Execute(lxxxtude)
    If regRes.Count < 1 Then
        g_tool_isLongitudeFormat = False
        Exit Function
    End If

End Function

Private Function g_tool_isLatitudeFormat(lxxxtude) As Boolean
    '判断纬度是否合法  -90 - +90  1-6位小数数字
    g_tool_isLatitudeFormat = True

    If Trim(lxxxtude) = "" Then
        g_tool_isLatitudeFormat = False   '''排空
        Exit Function
    End If

    Set latFormat = CreateObject("VBscript.regexp")
    latFormat.Pattern = "^[\-\+]?([0-8]?\d{1}\.\d{1,6}|90\.0{1,6})$"
    latFormat.IgnoreCase = True

    Set regRes = latFormat.Execute(lxxxtude)
    If regRes.Count < 1 Then
        g_tool_isLatitudeFormat = False
        Exit Function
    End If

End Function

该代码,1-6位小数,但是必须要有小数点,纯证书的经纬度比如经度120,纬度60,就通不过验证

vieyahn2017 commented 4 years ago

vba代码修改如下

Private Function g_tool_isLongitudeFormat(lxxxtude) As Boolean
    '判断经度是否合法  -180 - +180  纯整数,或者1-6位小数数字
    g_tool_isLongitudeFormat = True

    If Trim(lxxxtude) = "" Then
        g_tool_isLongitudeFormat = False   '''排空
        Exit Function
    End If

    Set lngFormat = CreateObject("VBscript.regexp")
    'lngFormat.Pattern = "^[\-\+]?(0?\d{1,2}\.\d{1,6}|1[0-7]?\d{1}\.\d{1,6}|180\.0{1,6}$"
    lngFormat.Pattern = "^[\-\+]?(0?\d{1,2})(\.\d{1,6}|1[0-7]?\d{1}\.\d{1,6}|180\.0{1,6})?$"
    lngFormat.IgnoreCase = True

    Set regRes = lngFormat.Execute(lxxxtude)
    If regRes.Count < 1 Then
        g_tool_isLongitudeFormat = False
        Exit Function
    End If

End Function

Private Function g_tool_isLatitudeFormat(lxxxtude) As Boolean
    '判断纬度是否合法  -90 - +90  纯整数,或者1-6位小数数字
    g_tool_isLatitudeFormat = True

    If Trim(lxxxtude) = "" Then
        g_tool_isLatitudeFormat = False   '''排空
        Exit Function
    End If

    Set latFormat = CreateObject("VBscript.regexp")
    'latFormat.Pattern = "^[\-\+]?([0-8]?\d{1}\.\d{1,6}|90\.0{1,6})$"
    latFormat.Pattern = "^[\-\+]?([0-8]?\d{1})(\.\d{1,6}|90\.0{1,6}))?$"
    latFormat.IgnoreCase = True

    Set regRes = latFormat.Execute(lxxxtude)
    If regRes.Count < 1 Then
        g_tool_isLatitudeFormat = False
        Exit Function
    End If

End Function

该代码,在经纬度取值要求内,整数,或者1-6位小数,都符合要求

vieyahn2017 commented 4 years ago

上面的有问题,比如178.37 这个经度匹配失败

vieyahn2017 commented 4 years ago

最新改正版:


Public Function g_tool_isLongitudeFormat(lxxxtude) As Boolean
    '判断经度是否合法  -180 - +180  纯整数,或者1-6位小数数字
    g_tool_isLongitudeFormat = True

    If Trim(lxxxtude) = "" Then
        g_tool_isLongitudeFormat = False   '''排空
        Exit Function
    End If

    Set lngFormat = CreateObject("VBscript.regexp")
    lngFormat.Pattern = "^[\-\+]?(0?\d{1,2}|1[0-7]\d{1}|180)?(\.\d{1,6})?$"
    lngFormat.IgnoreCase = True

    Set regRes = lngFormat.Execute(lxxxtude)
    If regRes.Count < 1 Then
        g_tool_isLongitudeFormat = False
        Exit Function
    End If

End Function

Public Function g_tool_isLatitudeFormat(lxxxtude) As Boolean
    '判断纬度是否合法  -90 - +90  纯整数,或者1-6位小数数字
    g_tool_isLatitudeFormat = True

    If Trim(lxxxtude) = "" Then
        g_tool_isLatitudeFormat = False   '''排空
        Exit Function
    End If

    Set latFormat = CreateObject("VBscript.regexp")
    latFormat.Pattern = "^[\-\+]?([0-8]?\d{1}|90)(\.\d{1,6})?$"
    latFormat.IgnoreCase = True

    Set regRes = latFormat.Execute(lxxxtude)
    If regRes.Count < 1 Then
        g_tool_isLatitudeFormat = False
        Exit Function
    End If

End Function

还是有点瑕疵 180.023这种经度还是被判为合法, (181.23不会被判为合法)