Match geographic coordinates based on degrees with regular expression

I would like to be able to identify form templates

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

Here is what I still have:

use utf8;
qr{
    (?:
    \d{1,3} \s*  °   \s*
    \d{1,2} \s*  '   \s*
    \d{1,2} \s*  "   \s*
    [ENSW]  \s* \.?
            \s*  ,?  \s*
    ){2}
}x;

Needless to say, this is not consistent. Does this have anything to do with extended symbols (namely, the degree symbol)? Or am I just pulling it in a big time?

I also appreciate directions CPANif you know something there that solves my problem. I looked at Regex :: Common and Geo :: Formatter , but none of them do what I want. Any ideas?

Update

, use utf8 . , , , . use utf8 . , , utf8.

+3
4

use utf8.

0xB0 ( , , UTF8). 0xB0 " " UTF8; , , - 0xC2 0xF4. utf8 .

+1

:

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";
}

:

$ ./coord.pl 
match
+5

x qr.

+1

?: , , , . .

If all coordinates are fixed format, it unpackmay be the best way to get the desired values.

my @twoCoordinates = unpack 'A2xA2xA2xAx3A2xA2xA2xA', "28°44'30"N., 33°12'36"E.";

print "@twoCoordinates";  # returns '28 44 30 N 33 12 36 E'

If not, change the regex:

my @twoCoordinates = "28°44'30"N., 33°12'36"E." =~ /\w+/g;
+1
source

Source: https://habr.com/ru/post/1752399/


All Articles