Matching degree-based geographical coordinates with a regular expression

后端 未结 4 1257
长情又很酷
长情又很酷 2021-01-21 00:00

I\'d like to be able to identify patterns of the form

28°44\'30\"N., 33°12\'36\"E.

Here\'s what I have so far:

use utf8;
qr{
           


        
4条回答
  •  [愿得一人]
    2021-01-21 00:33

    This:

    use strict;
    use warnings;
    use utf8;
    my $re = qr{
        (?:
        \d{1,3} \s*  °   \s*
        \d{1,2} \s*  '   \s*
        \d{1,2} \s*  "   \s*
        [ENSW]  \s* \.?
                \s*  ,?  \s*
        ){2}
    }x;
    if (q{28°44'30"N., 33°12'36"E.} =~ $re) {
        print "match\n";
    } else {
        print "no match\n";
    }
    

    works:

    $ ./coord.pl 
    match
    

提交回复
热议问题