Degrees Minutes Seconds(DMS)RegEx [英] Degrees Minutes Seconds (DMS) RegEx

查看:183
本文介绍了Degrees Minutes Seconds(DMS)RegEx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式,我希望以各种方式匹配纬度/经度对,例如

I have a regular expression that I want to match a latitude/longitude pair in a variety of fashions, e.g.

123 34 42
-123* 34' 42"
123* 34' 42"
+123* 34' 42"
45* 12' 22"N
45 12' 22"S
90:00:00.0N

我希望能够将这些匹配为一对,以便

I want to be able to match these in a pair such that

90:00:00.0N 180:00:00.0E 是一个纬度/经度对。

90:00:00.0N 180:00:00.0E is a latitude/longitude pair.

45 * 12'22N 46 * 12'22E 是纬度/经度对(1度1度单元格)。

45* 12' 22"N 46* 12' 22"E is a latitude/longitude pair (1 degree by 1 degree cell).

123 * 34'42124 * 34'42是纬度/经度对

etc

使用下面的正则表达式,当我输入123时,它匹配。我想这是真的,因为123 00 00是一个有效的坐标。但是,我想使用这个正则表达式匹配上面相同格式的对

Using the below regular expression, when I type in 123, it matches. I suppose this is true since 123 00 00 is a valid coordinate. However, I want to use this regular expression to match pairs in the same format above

   "([-|\\+]?\\d{1,3}[d|D|\u00B0|\\s](\\s*\\d{1,2}['|\u2019|\\s])?"
 + "(\\s*\\d{1,2}[\"|\u201d|\\s])?\\s*([N|n|S|s|E|e|W|w])?\\s?)"

我使用的是Java。

* 表示学位。

我在正则表达式中做错了什么?

What am I doing wrong in my regular expression?

推荐答案

嗯,首先,你是用一堆不必要的管道字符填充你的字符集 - 在 [] 对中暗示了交替。额外的清理: + 不需要在字符类中进行转义。你的正则表达式似乎正在解决比你给我们更大的问题陈述 - 你没有提到 d D 作为匹配的角色。而且你已经完成了RegEx可选的整个后半部分。关于我认为原始问题陈述的内容,我构建了以下正则表达式:

Well, for one thing, you're filling your character sets with a bunch of unnecessary pipe characters - alternation is implied in a [] pair. Additional cleanup: + doesn't need to be escaped in a character class. Your regular expression seems to be addressing a bigger problem statement than you gave us - you make no mention of d or D as matchable character. And you've made pretty much the entire back half of your RegEx optional. Going off of what I think your original problem statement is, I built the following regular expression:

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

这有点像doozy,但是我会为你或者未来发生过这种情况的任何人(你好,未来!)打破它。 / p>

It's a bit of a doozy, but I'll break it down for you, or anyone who happens across this in the future (Hello, future!).

^

字符串的开头,简单。

\s*

任何数量的空白 - 甚至没有。

Any amount of whitespace - even none.

( 

表示群组的开头 - 我们会回到原点。

Denotes the beginning of a group - we'll get back to that.

[-+]?

可选符号

\d{1,3}

1到3位数

\*?

一个可选的星号 - 这里的转义是星号的关键,但如果你想用实际程度的unicode代码点替换它,你将不需要它。

An optional Asterisk - the escape here is key for an asterisk, but if you want to replace this with the unicode codepoint for an actual degree, you won't need it.

\s+

At至少一个空白字符

At least one character of whitespace

\d{1,2}

1位或2位数。

'?

可选撇号

\s+\d{1,2}+

你见过以前这些,但有一个新的曲线球 - 在 {1,2} 量词之后有一个加号!这使得它成为所有格量词,这意味着匹配器不会放弃该组的匹配以使另一个可能。这几乎完全是为了防止来自匹配的 1 1 11 1 1 ,但可以用来在100%确定你不需要能够的任何地方提高速度回溯。

You've seen these before, but there's a new curveball - there's a plus after the {1,2} quantifier! This makes it a possessive quantifier, meaning that the matcher won't give up its matches for this group to make another one possible. This is almost exclusively here to prevent 1 1 11 1 1 from matching, but can be used to increase speed anywhere you're 100% sure you don't need to be able to backtrack.

"?

可选的双引号。你必须用Java来逃避这一点。

Optional double quote. You'll have to escape this in Java.

[NSEW]?

一个可选的基数方向,用字母表示

An optional cardinal direction, designated by letter

|

或 - 您可以匹配组中的所有内容在此之前,或者此后组中的所有内容。

OR - you can match everything in the group before this, or everything in the group after this.

\d{1,3}

旧消息。

(:\d{2})

冒号,后跟两个字符......

A colon, followed by two characters...

{2}

两次!

\.\d

小数点,后跟一位数。

[NSEW]

与以前相同,但这次是强制性的。

Same as before, but this time it's mandatory.

\s*)

一些空间,最后是小组的结尾。现在,第一组已匹配整个经度/纬度表示,末尾有任意数量的空间。紧随其后:

Some space, and finally the end of the group. Now, the first group has matched an entire longitude/latitude denotation, with an arbitrary amount of space at the end. Followed closely by:

{1,2}

做一次或两次 - 以匹配单个或一对,最后:

Do that one, or two times - to match a single or a pair, then finally:

$

字符串的结尾。

这不是完美的,但它非常接近,我认为它回答了原始的问题陈述。另外,我觉得我的解释已经揭开神秘面纱,你可以编辑它以进一步满足你的需求。它不会(也不会)做的一件事就是强制第一个坐标与第二个坐标相匹配。这对于正则表达式来说太过分了。

This isn't perfect, but it's pretty close, and I think it answers the original problem statement. Plus, I feel my explanation has demystified it enough that you can edit it to further suit your needs. The one thing it doesn't (and won't) do, is enforce that the first coordinate matches the second in style. That's just too much to ask of Regular Expressions.

怀疑者:在这里正在行动。请享受。

这篇关于Degrees Minutes Seconds(DMS)RegEx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆