找到你要的答案

Q:Regex Validating Lat & long

Q:正则表达式验证LAT和长

I am a complete newbie to reg ex and have been tasked with trying to update our regex validators. Currently, they check that the lat and longs are real but now we need to make sure that they are within a certain box. Since a user can hand type it in or select it from a map we get them in different styles (minutes/degrees as well as decimal) that we need to account for.

Desired validation--

LAT 50 through 72 & LONG: -130 through -180 OR 172 through 180)

Current RegEx--

LAT:
"^(-?((90((\.0{1,15})|(\:00\:00))?)|(([0-8]?\d)((\.\d{1,15})|((\:[0-5]\d){2}))?)))$"

LONG: "^(-?((180((\.0{1,15})|(\:00\:00))?)|(((\d{1,2})|(1[0-7]\d))((\.\d{1,15})|((\:[0-5]\d){2}))?)))$"

I did try to google it and was playing around with this

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

but I am having a problem even reading it so I can't tell how to update it:

Typical inputs: Latitude could be either 52.5 OR 52:30:00

I am a complete newbie to reg ex and have been tasked with trying to update our regex validators. Currently, they check that the lat and longs are real but now we need to make sure that they are within a certain box. Since a user can hand type it in or select it from a map we get them in different styles (minutes/degrees as well as decimal) that we need to account for.

所需的验证——

50通过72 &;长:130通过180或172到180)

当前的正则表达式--

LAT:
"^(-?((90((\.0{1,15})|(\:00\:00))?)|(([0-8]?\d)((\.\d{1,15})|((\:[0-5]\d){2}))?)))$"

LONG: "^(-?((180((\.0{1,15})|(\:00\:00))?)|(((\d{1,2})|(1[0-7]\d))((\.\d{1,15})|((\:[0-5]\d){2}))?)))$"

我确实尝试谷歌和它玩这个

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

但我有一个问题,甚至阅读它,所以我不知道如何更新它:

典型的输入:纬度可以是52.5或52:30:00

answer1: 回答1:

If regex had to be used (as mentioned in your comment above) I would probably go with something like this for "lat" with a range of 50 through 72:

^(?:(?:[56]\d|7[01])(?:\.\d{1,15}|(?::(?:[0-5]\d|\d)){1,2})?|72(?:\.0{1,15}|(?::00){1,2})?)$

Debuggex Demo

Note I assume here that your example 52:30:00 means that the two number components after the colon must be in the 0 to 59 range.

I'll leave the "long" input checking up to you... but I would highlight that your attempt has some issues:

^[+]?([1-8]?\d(\.\d+)?|50(\.0+)?),\s*[+]?(72(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$
  • it includes the + symbol - this was not mentioned in your example inputs
  • it includes , and white-space \s* detection - why is this here - again your example inputs don't allow the user to enter comma or white space.

Focus on writing a regex for one range at a time, -130 through -180 first then 172 through 180, then combine the two seperate regular expressions with a |.

You also didn't mention which language your regular expression was for - I assumed JavaScript - but you should be careful if this isn't the case - and more clear in future questions.

Hope this helps.

如果正则表达式必须使用(如上所述在你上面的评论)我会有这样的“土地增值税”的范围从50到72:

^(?:(?:[56]\d|7[01])(?:\.\d{1,15}|(?::(?:[0-5]\d|\d)){1,2})?|72(?:\.0{1,15}|(?::00){1,2})?)$

debuggex演示

注意,我假定你的例子52:30:00意味着这两个号码组成冒号后必须在0到59的范围内。

我将离开“长”输入检查你…但我会强调你的尝试有一些问题:

^[+]?([1-8]?\d(\.\d+)?|50(\.0+)?),\s*[+]?(72(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$
  • it includes the + symbol - this was not mentioned in your example inputs
  • it includes , and white-space \s* detection - why is this here - again your example inputs don't allow the user to enter comma or white space.

写一个正则表达式的一个范围,在一个时间集中,130通180先172到180,两个正则表达式与|结合。

你还没说你的语言的正则表达式是我认为JavaScript -但你应该如果情况不是这样的小心和未来的问题更加清晰。

希望这有助于。

regex  latitude-longitude