.Net Regex:匹配但不包括部分匹配 [英] .Net Regex: Match but don't include part of the match

查看:75
本文介绍了.Net Regex:匹配但不包括部分匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个正则表达式,我需要匹配,以几种不同的格式搜索纬度经度点。这个问题是它会匹配不应该作为坐标的数字的部分坐标。



正则表达式:

I have a regular expression that I need to match which searches for a Latitude Longitude point in a couple of different formats. The problem with this is that it will match partial coordinates out of numbers that shouldn't work as coordinates.

Regex:

([1-8]?[0-9]\.[\d]{1,10}|90\.0+|0\.[\d]{1,10})\s?°?\s?[NS]?\s?\,\s*(([1][8][0]\.0+)|(([0-9]|[1-9][0-9]|1[0-7][0-9]|[0])\.[\d]{1,10}))\s?°?\s?[EW]?





例如,上面的正则表达式将匹配字符串中的90.0000,180.0000 190.0000,180.0000这不是一个有效的坐标。我想出来解决这个问题的方法是将正则表达式放在非数字字符匹配中:



For Example, the above regex will match 90.0000, 180.0000 out of the string "190.0000, 180.0000" which isn't a valid coordinate. The way I've come up with to combat this is placing the Regex inside non-digit character matches:

[^\d]...regex...[^\d]

就像魅力一样。



现在的问题是数字包含在比赛中以及我搜索的时候多个匹配它开始跳过它们之间只有一个空格的坐标(就像一个新行)。



所以在搜索字符串时:

which works like a charm.

The problem with this is now the digits are included in the match and when I am searching for multiple matches it starts skipping coordinates that only have one white space in between them (like a new line).

So when searching through the string:

"90.0000°, 180.0000°
78.4566°, 177.4444°
12.12345N, 80.676667E"



它将跳过78.4566°,177.4444°。



我的问题是:

有没有办法匹配坐标(在两个[^ \d]之间,但不包括[ ^ \d]在比赛中所以正则表达式搜索会在最后一场比赛的最后一次[^ \d]开始测试下一场比赛吗?




-Kyle



这是针对VB.Net 3.5+正则表达式


it will skip 78.4566°, 177.4444°.

My Question is:
Is there a way to match the coordinate (in between two [^\d] but don't include [^\d] in the match so the regex search will begin testing the next match at the last [^\d] in the last match?


-Kyle

This is for a VB.Net 3.5+ Regex

推荐答案

尝试这个,而不是用[^ \d]封闭你的exp ...正则表达式... [^ \d]





Try this instead of enclosing your exp with [^\d]...regex...[^\d]


(?<!\d)([1-8]?[0-9]\.[\d]{1,10}|90\.0+|0\.[\d]{1,10})\s?°?\s?[NS]?\s?\,\s*(([1][8][0]\.0+)|(([0-9]|[1-9][0-9]|1[0-7][0-9]|[0])\.[\d]{1,10}))\s?°?\s?[EW]?



我在开头就把'负面看法'置于零长度断言之后


I've put a 'negative look behind' zero length assertion at the begining

(?<!\d)





有关零长度断言的更多信息,请参阅msdn或

http://www.regular-expressions.info/lookaround.html [ ^ ]


这篇关于.Net Regex:匹配但不包括部分匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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