开发者

Regex regular expression coordinates

开发者 https://www.devze.com 2023-04-07 13:08 出处:网络
trying to sort out a regular expression for the following string: 51.4920302, -0.0850667 So far I have:@\"^[0-9]*,{-}[0-9]*$\" but it doesn\'t seem to be working.

trying to sort out a regular expression for the following string:

51.4920302, -0.0850667

So far I have:@"^[0-9]*,{-}[0-9]*$" but it doesn't seem to be working.

Any thought, greatly received.

The whole sni开发者_开发百科ppet is:

[RegularExpression(@"^[0-9]*,{-}[0-9]*$", ErrorMessage = "Must enter a valid coordinate")]
    public string FaveRunLatLng2 { get; set; }

Thanks.


You're not allowing for the decimal point. You're also basically requiring the second part of the coordinates to be negative and not allowing the first. Try

@"^-?[0-9]+\.[0-9]+, -?[0-9]+\.[0-9]+$"


\b-?\d+\.\d+, -?\d+\.\d+\b

If you want the space to be optional, you can add \s?, like this:

\b-?\d+\.\d+,\s?-?\d+\.\d+\b

As long as you know your input is going to have a comma and space. If it is entered by a user, you may need to sanitize it first.

Here is a tester online you can use: http://www.regular-expressions.info/javascriptexample.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号