.NET REGEX 匹配匹配空字符串
我有这样的
模式:
[0-9]*\.?[0-9]*
目标:
X=113.3413475 Y=18.2054775
我想匹配数字。它与 http://regexpal.com/ 和 Regex Coach 等测试软件中的查找相匹配。
但在 Dot net 和 http:// derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
我得到:
Found 11 matches:
1.
2.
3.
4.
5.
6. 113.3413475
7.
8.
9.
10. 18.2054775
11.
String literals for use in programs:
C#
@"[0-9]*[\.]?[0-9]*"
任何人都知道我为什么会这样所有这些空匹配。
谢谢和问候, 凯文
I have this
pattern:
[0-9]*\.?[0-9]*
Target:
X=113.3413475 Y=18.2054775
And i want to match the numbers. It matches find in testing software like http://regexpal.com/ and Regex Coach.
But in Dot net and http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx
I get:
Found 11 matches:
1.
2.
3.
4.
5.
6. 113.3413475
7.
8.
9.
10. 18.2054775
11.
String literals for use in programs:
C#
@"[0-9]*[\.]?[0-9]*"
Any one have any idea why i'm getting all these empty matches.
Thanks and Regards,
Kevin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,这将匹配空字符串。看看它:
一切都是可选的,所以空字符串匹配。
听起来你总是希望有数字某处,例如:(
这里的顺序很重要,因为你希望它采取最可能的方式。)
这对我有用:
输出:
很可能诚然,更好的方法:)
Yes, that will match empty string. Look at it:
Everything's optional, so an empty string matches.
It sounds like you always want there to be digits somewhere, for example:
(The order here matters, as you want it to take the most possible.)
That works for me:
Output:
There may well be better ways of doing it, admittedly :)
另一种选择是保留原始的正则表达式,并断言其中必须有一个数字(可能在点之后):
转到:
Another alternative is to keep your original regex, and just assert it must have a number in it (maybe after a dot):
Goes to:
试试这个:
它与 Jon Skeet 的答案略有不同,因为它不会匹配
.45
,它需要单独的数字(例如8
)或真正的小数(例如8.1
或0.1
)Try this one:
It's slightly different that Jon Skeet's answer in that it won't match
.45
, it requires either a number alone (e.g.8
) or a real decimal (e.g.8.1
or0.1
)关键问题是
*
,它的意思是“匹配零个或多个前面的字符”。空字符串匹配零个或多个数字,这就是您获得所有这些匹配项的原因。将两个
*
更改为+
,您将得到您想要的。The key problem is the
*
, which means "match zero or more of the preceding characters". The empty string matches zero or more digits, which is why you're getting all those matches.Change your two
*
s to+
s and you'll get what you want.这个正则表达式的问题在于它在所有字段中都是完全可选的,因此它也会匹配空字符串。我会考虑添加所有案例。通过正则表达式,我看到您想要带或不带点的数字,以及带或不带一组十进制数字的数字。您可以首先分隔那些仅包含数字
[0-9]+
的内容,然后分隔那些包含数字加一个点[0-9]+\.
的内容,然后使用|
(或)将它们全部连接起来。正则表达式的问题在于它允许非实数的情况,例如,第一组数字和最后一组数字为空(只是一个点)的情况),所以你必须明确地提出有效的情况。
The problem with this regex is that it is completely optional in all the fields, so an empty string also is matched by it. I would consider adding all the cases. By the regex, I see you want the numbers with or without dot, and with or without a set of decimal digits. You can separate first those that contain only numbers
[0-9]+
, then those that contain numbers plus only a dot,[0-9]+\.
and then join them all with|
(or).The problem with the regex as it is is that it allows cases that are not real numbers, for example, the cases in which the first set of numbers and the last set of numbers are empty (just a dot), so you have to put the valid cases explicitly.
将 * 替换为 + 并删除 ?从你的月经情况来看。
编辑:从上面的对话:@“[0-9]+。[0-9]*|。[0-9]+|[0-9]+”,是更好的情况。渔获量 123、.123、123.123 等
Replace your * with + and remove ? from your period case.
EDIT: from above conversation: @"[0-9]+.[0-9]*|.[0-9]+|[0-9]+", is the better case. catches 123, .123, 123.123 etc