在perl中分割包含经度或纬度表达式的字符串 [英] Splitting a string containing a longitude or latitude expression in perl
问题描述
我从包含真实测地线表达式的网络中检索数据,即我用Unicode符号U+00B0, U+2032 and U+2033
表示度,分和秒,分别命名为度,素数和双素数.示例:
I retrieve data from the net containing real geodesic expressions, by that I mean degrees, minutes and seconds with Unicode symbols: U+00B0, U+2032 and U+2033
, named Degree, Prime and Double Prime. Example:
my $Lat = "48° 25′ 43″ N";
我的目标是首先将这样的表达式转换为度,然后转换为弧度,以在我正在编写的Perl模块中使用,该模块实现了Vincenty逆公式来计算椭球大圆距离.我所有的代码目标都已达到伪大地测量学的要求,例如"48:25:43 N",但是,当然,这是手工输入的测试数据,而不是真实世界的数据.我正在努力制作一个正则表达式,该正则表达式可以像现在处理伪数据一样拆分此真实数据,如下所示:
My objective is to convert such an expression first to degrees and then to radians to be used in a Perl module I am writing that implements the Vincenty inverse formula to calculate ellipsoidal great-circle distances. All my code objectives have been met with pseudo geodesics, such as "48:25:43 N", but of course, this is hand entered test data, not real world data. I am struggling with crafting a regular expression that can split this real data as I now do pseudo data, as in:
my ($deg, $min, $sec, $dir) = split(/[\s:]+/, $_[0], 4); # this works
我尝试了很多正则表达式,包括
I have tried many regular expressions including
/[°′″\s]+/ and
/[\x{0B00}\x{2032}\x{2033}\s]/+
所有结果令人沮丧,例如$deg = "48?", $min = "?", $sec = "25′43″ N" and $dir = undef
.我已经将代码封装在大括号{}
中,并使用utf8包含在该范围内;并使用功能'unicode_strings';都具有nada结果.
all with dismal results, such as $deg = "48?", $min = "?", $sec = "25′43″ N" and $dir = undef
. I've encapsulated the code inside braces {}
and included within that scope use utf8; and use feature 'unicode_strings'; all with nada results.
输入数据示例:
my $Lat = "48° 25′ 43″ N";
预期输出:
$deg = 48, $min = 25, $sec = 43 and $dir = "N"
推荐答案
您可以尝试使用此正则表达式来拆分字符串:
You may try this regex to split the string:
[^\dNSEW.]+
示例来源:( 在此处运行)
my $str = '48° 25′ 43″ N';
my $regex = qr/[^\dNSEW.]+/p;
my ($deg, $min, $sec, $dir) = split $regex, $str;
这篇关于在perl中分割包含经度或纬度表达式的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!