.NET REGEX 匹配匹配空字符串

发布于 2024-12-03 12:35:06 字数 709 浏览 1 评论 0原文

我有这样的

模式:

[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浮华 2024-12-10 12:35:06

是的,这将匹配空字符串。看看它:

[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);
        }
    }
}

输出:

113.3413475
18.2054775

很可能诚然,更好的方法:)

Yes, that will match empty string. Look at it:

[0-9]* - zero or more digits
\.?    - an optional period
[0-9]* - zero or more digits

Everything's optional, so an empty string matches.

It sounds like you always want there to be digits somewhere, for example:

[0-9]+\.[0-9]*|\.[0-9]+|[0-9]+

(The order here matters, as you want it to take the most possible.)

That works for me:

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

There may well be better ways of doing it, admittedly :)

木格 2024-12-10 12:35:06

另一种选择是保留原始的正则表达式,并断言其中必须有一个数字(可能在点之后):

[0-9]*\.?[0-9]*

转到:

(?=\.?[0-9])[0-9]*\.?[0-9]*

Another alternative is to keep your original regex, and just assert it must have a number in it (maybe after a dot):

[0-9]*\.?[0-9]*

Goes to:

(?=\.?[0-9])[0-9]*\.?[0-9]*
情定在深秋 2024-12-10 12:35:06

试试这个:

[0-9]+(\.[0-9]+)?

它与 Jon Skeet 的答案略有不同,因为它不会匹配 .45,它需要单独的数字(例如 8)或真正的小数(例如 8.10.1

Try this one:

[0-9]+(\.[0-9]+)?

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 or 0.1)

燕归巢 2024-12-10 12:35:06

关键问题是 *,它的意思是“匹配零个或多个前面的字符”。空字符串匹配零个或多个数字,这就是您获得所有这些匹配项的原因。

将两个 * 更改为 +,您将得到您想要的。

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.

も星光 2024-12-10 12:35:06

这个正则表达式的问题在于它在所有字段中都是完全可选的,因此它也会匹配空字符串。我会考虑添加所有案例。通过正则表达式,我看到您想要带或不带点的数字,以及带或不带一组十进制数字的数字。您可以首先分隔那些仅包含数字 [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.

っ〆星空下的拥抱 2024-12-10 12:35:06
Regex pattern = new Regex( @"[0-9]+[\.][0-9]+");

string info = "X=113.3413475 Y=18.2054775";

MatchCollection matches = pattern.Matches(info);

int count = 1;
foreach(Match match in matches)
{
    Console.WriteLine("{0} : {1}", count++, match.Value);
}

//output
//1 : 113.3413475
//2 : 18.2054775

将 * 替换为 + 并删除 ?从你的月经情况来看。

编辑:从上面的对话:@“[0-9]+。[0-9]*|。[0-9]+|[0-9]+”,是更好的情况。渔获量 123、.123、123.123 等

Regex pattern = new Regex( @"[0-9]+[\.][0-9]+");

string info = "X=113.3413475 Y=18.2054775";

MatchCollection matches = pattern.Matches(info);

int count = 1;
foreach(Match match in matches)
{
    Console.WriteLine("{0} : {1}", count++, match.Value);
}

//output
//1 : 113.3413475
//2 : 18.2054775

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文