代码之家  ›  专栏  ›  技术社区  ›  Jake Petroules

用于匹配纬度/经度坐标的正则表达式?

  •  109
  • Jake Petroules  · 技术社区  · 14 年前

    我正在尝试创建一个正则表达式来匹配纬度/经度坐标。匹配我用过的双精度数字 (\-?\d+(\.\d+)?)

    ^(\-?\d+(\.\d+)?),\w*(\-?\d+(\.\d+)?)$
    

    我希望这个匹配一个双精度,一个逗号,也许一些空格,和另一个双精度,但它似乎不工作。具体来说,它只在没有空间的情况下工作,而不是一个或多个空间。我做错了什么?

    18 回复  |  直到 14 年前
        1
  •  126
  •   Miha_x64    5 年前

    空白是\s,不是\w

    ^(-?\d+(\.\d+)?),\s*(-?\d+(\.\d+)?)$
    

        2
  •  237
  •   Iain Fraser    10 年前

    ^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$
    

    比赛

    • +90.0, -127.554334
    • 45, 180
    • -90.000, -180.0000
    • 47.1231231, 179.99999999

    • -90., -180.
    • +90.1, -100.111
    • -91, 123.456
    • 045, 180
        3
  •  122
  •   Marco Ferrari    8 年前

    我使用的是这些数字(十进制格式,有6个十进制数字):

    ^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,6})?))$
    

    Latitude Regular expression visualization

    经度

    ^(\+|-)?(?:180(?:(?:\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\.[0-9]{1,6})?))$
    

    Longitude Regular expression visualization


    Here

    import static org.hamcrest.Matchers.*;
    import static org.hamcrest.MatcherAssert.*;
    
    import java.math.RoundingMode;
    import java.text.DecimalFormat;
    
    import lombok.extern.slf4j.Slf4j;
    
    import org.testng.annotations.Test;
    
    @Slf4j
    public class LatLongValidationTest {
    
        protected static final String LATITUDE_PATTERN="^(\\+|-)?(?:90(?:(?:\\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\\.[0-9]{1,6})?))$";
        protected static final String LONGITUDE_PATTERN="^(\\+|-)?(?:180(?:(?:\\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\\.[0-9]{1,6})?))$";
    
        @Test
        public void latitudeTest(){
            DecimalFormat df = new DecimalFormat("#.######");
            df.setRoundingMode(RoundingMode.UP);
            double step = 0.01;
            Double latitudeToTest = -90.0;
    
            while(latitudeToTest <= 90.0){
                boolean result = df.format(latitudeToTest).matches(LATITUDE_PATTERN);
                log.info("Latitude: tested {}. Result (matches regex): {}", df.format(latitudeToTest), result);
                assertThat(result, is(true));
                latitudeToTest += step;
            }
    
            latitudeToTest = -90.1;
    
            while(latitudeToTest >= -200.0){
                boolean result = df.format(latitudeToTest).matches(LATITUDE_PATTERN);
                log.info("Latitude: tested {}. Result (matches regex): {}", df.format(latitudeToTest), result);
                assertThat(result, is(false));
                latitudeToTest -= step;
            }
    
            latitudeToTest = 90.01;
    
            while(latitudeToTest <= 200.0){
                boolean result = df.format(latitudeToTest).matches(LATITUDE_PATTERN);
            log.info("Latitude: tested {}. Result (matches regex): {}", df.format(latitudeToTest), result);
                assertThat(result, is(false));
                latitudeToTest += step;
            }
        }
    
        @Test
        public void longitudeTest(){
            DecimalFormat df = new DecimalFormat("#.######");
            df.setRoundingMode(RoundingMode.UP);
            double step = 0.01;
            Double longitudeToTest = -180.0;
    
            while(longitudeToTest <= 180.0){
                boolean result = df.format(longitudeToTest).matches(LONGITUDE_PATTERN);
                log.info("Longitude: tested {}. Result (matches regex): {}", df.format(longitudeToTest), result);
                assertThat(result, is(true));
                longitudeToTest += step;
            }
    
            longitudeToTest = -180.01;
    
            while(longitudeToTest >= -300.0){
                boolean result = df.format(longitudeToTest).matches(LONGITUDE_PATTERN);
                log.info("Longitude: tested {}. Result (matches regex): {}", df.format(longitudeToTest), result);
                assertThat(result, is(false));
                longitudeToTest -= step;
            }
    
            longitudeToTest = 180.01;
    
            while(longitudeToTest <= 300.0){
                boolean result = df.format(longitudeToTest).matches(LONGITUDE_PATTERN);
                log.info("Longitude: tested {}. Result (matches regex): {}", df.format(longitudeToTest), result);
                assertThat(result, is(false));
                longitudeToTest += step;
            }
        }
    }
    
        4
  •  20
  •   Nicolas Raoul    11 年前

    经度测量范围从180°到+180°


    而且,按照我的想法,没有人应该限制纬度/经度的小数点。

    ^([-+]?\d{1,2}([.]\d+)?),\s*([-+]?\d{1,3}([.]\d+)?)$
    

    或目标C

    ^([-+]?\\d{1,2}([.]\\d+)?),\\s*([-+]?\\d{1,3}([.]\\d+)?)$
    
        5
  •  16
  •   Jonatas Walker    9 年前
    ^-?[0-9]{1,3}(?:\.[0-9]{1,10})?$
    

    正则表达式细分:

    ^-?[0-9]{1,3}(?:\.[0-9]{1,10})?$
    

    -? #接受负值

    ^

    [0-9]{1,3} #匹配1-3个数字(即0-999)

    (?: #尝试匹配。。。

    \. #小数点

    [0-9]{1,10}

    )? #…可选

    $ #字符串结尾

        6
  •  9
  •   user1439929    11 年前

    ^(\()([-+]?)([\d]{1,2})(((\.)(\d+)(,)))(\s*)(([-+]?)([\d]{1,3})((\.)(\d+))?(\)))$
    

    请访问:

    http://regexpal.com/

    将表达式粘贴到顶部框中,然后将以下内容放入底部框中:

    (80.0123, -34.034)
    (80.0123)
    (80.a)
    (980.13, 40)
    (99.000, 122.000)
    

    正则表达式细分:

    ^                    # The string must start this way (there can't be anything before). 
        (\()             # An opening parentheses (escaped with a backslash).
        ([-+]?)          # An optional minus, or an optional plus.
        ([\d]{1,2})      # 1 or 2 digits (0-9).
        (                # Start of a sub-pattern.
            (            # Start of a sub-pattern.
                (\.)     # A dot (escaped with a backslash).
                (\d+)    # One or more digits (0-9).
                (,)      # A comma.
            )            # End of a sub-pattern.
        )                # End of a sub-pattern.
        (\s*)            # Zero or more spaces.
        (                # Start of a sub-pattern.
            ([-+]?)      # An optional minus, or an optional plus. 
            ([\d]{1,3})  # 1 to 3 digits (0-9).
            (            # Start of a pattern.
                (\.)     # A dot (escaped with a backslash).
                (\d+)    # One or more digits (0-9).
            )?           # End of an optional pattern.
            (\))         # A closing parenthesis (escaped with a backkslash).
        )                # End of a pattern
    $                    # The string must end this way (there can't be anything after).
    

    现在,它没有做的是将自己限制在这个范围内:

    (-90 to +90, and -180 to +180)
    

    相反,它仅限于此范围:

    (-99 to +99, -199 to +199) 
    

    但重点主要是分解每一个表达。

        7
  •  8
  •   Alix Axel    12 年前

    这里有一个更严格的版本:

    ^([-+]?\d{1,2}[.]\d+),\s*([-+]?\d{1,3}[.]\d+)$
    
    • 纬度= -90 -- +90
    • 经度= -180 -- +180
        8
  •  7
  •   Arun Karunagath    7 年前

    @macro-ferrari 我确实找到了一个缩短它的方法,而且根据最近所有关于 regex engines

    const LAT_RE = /^[+-]?(([1-8]?[0-9])(\.[0-9]{1,6})?|90(\.0{1,6})?)$/;
    

    enter image description here

    const LONG_RE = /^[+-]?((([1-9]?[0-9]|1[0-7][0-9])(\.[0-9]{1,6})?)|180(\.0{1,6})?)$/;
    

    enter image description here

        9
  •  5
  •   picmate æ¶    10 年前

    纬度: result = re.match("^[+-]?((90\.?0*$)|(([0-8]?[0-9])\.?[0-9]*$))", '-90.00001')

    result = re.match("^[+-]?((180\.?0*$)|(((1[0-7][0-9])|([0-9]{0,2}))\.?[0-9]*$))", '-0.0000')

    在这个例子中,纬度应该失败。

        10
  •  3
  •   theraccoonbear    14 年前

    我相信您使用的是\w(单词字符),而您应该使用\s(空格)。单词字符通常由[A-Za-z0-9_304;]组成,这样就排除了空格,而空格在可选的减号或数字上进一步无法匹配。

        11
  •  3
  •   kenorb    5 年前

    这适用于这样的格式:3137.4'E

    ^[-]?\d{1,2}[ ]*ͦ[ ]*\d{1,2}\.?\d{1,2}[ ]*\x27[ ]*\w$
    
        12
  •  1
  •   Zoran Kikic    10 年前

    红宝石

    经度

    /^(-?(?:1[0-7]|[1-9])?\d(?:\.\d{1,8})?|180(?:\.0{1,8})?)$/ === longitude.to_s
    

    纬度

    /^(-?[1-8]?\d(?:\.\d{1,8})?|90(?:\.0{1,8})?)$/ === latitude.to_s
    
        13
  •  0
  •   saxenarishav    6 年前

    在目标C中,检查经纬度的正确模式的一个完整而简单的方法是:

     -( BOOL )textIsValidValue:(NSString*) searchedString
    {
        NSRange   searchedRange = NSMakeRange(0, [searchedString length]);
        NSError  *error = nil;
        NSString *pattern = @"^[-+]?([1-8]?\\d(\\.\\d+)?|90(\\.0+)?),\\s*[-+]?(180(\\.0+)?|((1[0-7]\\d)|([1-9]?\\d))(\\.\\d+)?)$";
        NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern: pattern options:0 error:&error];
        NSTextCheckingResult *match = [regex firstMatchInString:searchedString options:0 range: searchedRange];
        return match ? YES : NO;
    }
    

    哪里 搜索字符串 是用户将在相应文本字段中输入的输入。

        14
  •  0
  •   kenorb    6 年前

    $latitude $longitude

    $latitude_pattern  = '/\A[+-]?(?:90(?:\.0{1,18})?|\d(?(?<=9)|\d?)\.\d{1,18})\z/x';
    $longitude_pattern = '/\A[+-]?(?:180(?:\.0{1,18})?|(?:1[0-7]\d|\d{1,2})\.\d{1,18})\z/x';
    if (preg_match($latitude_pattern, $latitude) && preg_match($longitude_pattern, $longitude)) {
      // Valid coordinates.
    }
    
        15
  •  0
  •   Ivan Sanz Carasa    4 年前

    (?<latitude>-?\d+\.\d{3,10}),(?<longitude>-?\d+\.\d{3,10})

        16
  •  -1
  •   Giordano Prashanth Babu    8 年前

    你可以试试这个:

    var latExp = /^(?=.)-?((8[0-5]?)|([0-7]?[0-9]))?(?:\.[0-9]{1,20})?$/;
    var lngExp = /^(?=.)-?((0?[8-9][0-9])|180|([0-1]?[0-7]?[0-9]))?(?:\.[0-9]{1,20})?$/;
    
        17
  •  -2
  •   Giordano Prashanth Babu    8 年前

    ^[-+]?(([0-8]\\d|\\d)(\\.\\d+)?|90(\\.0+)?)$,\s*^[-+]?((1[0-7]\\d(\\.\\d+)?)|(180(\\.0+)?)|(\\d\\d(\\.\\d+)?)|(\\d(\\.\\d+)?))$
    
        18
  •  -2
  •   Giordano Prashanth Babu    8 年前

    试试这个:

    (?<!\d)([-+]?(?:[1-8]?\d(?:\.\d+)?|90(?:\.0+)?)),\s*([-+]?(?:180(?:\.0+)?|(?:(?:1[0-7]\d)|(?:[1-9]?\d))(?:\.\d+)?))(?!\d)`