我有这个
pattern:
[0-9]*\.?[0-9]*
Target:
X=113.3413475 Y=18.2054775
我想匹配数字。它与测试软件中的查找相匹配,例如http://regexpal.com/ http://regexpal.com/和正则表达式教练。
但在 Dot net 和http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx 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]*"
任何人都知道为什么我会收到所有这些空火柴。
感谢致敬,
凯文
是的,这将匹配空字符串。看它:
[0-9]* - zero or more digits
\.? - an optional period
[0-9]* - zero or more digits
一切都是可选的,因此空字符串匹配。
听起来你总是希望有数字某处, 例如:
[0-9]+\.[0-9]*|\.[0-9]+|[0-9]+
(这里的顺序很重要,因为您希望它采取尽可能多的顺序。)
这对我行得通:
using System;
using System.Text.RegularExpressions;
class Test
{
static void Main(string[] args)
{
string x = "X=113.3413475 Y=18.2054775";
Regex regex = new Regex(@"[0-9]+\.[0-9]*|\.[0-9]+|[0-9]+");
var matches = regex.Matches(x);
foreach (Match match in matches)
{
Console.WriteLine(match);
}
}
}
Output:
113.3413475
18.2054775
诚然,很可能有更好的方法:)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)