C# 仅用于数字的正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/273141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:03:52  来源:igfitidea点击:

Regex for numbers only

c#regex

提问by Timothy Carter

I haven't used regular expressions at all, so I'm having difficulty troubleshooting. I want the regex to match only when the contained string is all numbers; but with the two examples below it is matching a string that contains all numbers plus an equals sign like "1234=4321". I'm sure there's a way to change this behavior, but as I said, I've never really done much with regular expressions.

我根本没有使用过正则表达式,所以我很难排除故障。我希望正则表达式仅在包含的字符串全部为数字时匹配;但是在下面的两个示例中,它匹配包含所有数字和等号的字符串,例如“1234=4321”。我确信有一种方法可以改变这种行为,但正如我所说,我从来没有对正则表达式做过很多事情。

string compare = "1234=4321";
Regex regex = new Regex(@"[\d]");

if (regex.IsMatch(compare))
{ 
    //true
}

regex = new Regex("[0-9]");

if (regex.IsMatch(compare))
{ 
    //true
}

In case it matters, I'm using C# and .NET2.0.

以防万一,我使用的是 C# 和 .NET2.0。

采纳答案by Bill the Lizard

Use the beginning and end anchors.

使用开始和结束锚点。

Regex regex = new Regex(@"^\d$");

Use "^\d+$"if you need to match more than one digit.

使用"^\d+$",如果你需要匹配一个以上的数字。



Note that "\d"will match [0-9]and other digit characters like the Eastern Arabic numerals ??????????. Use "^[0-9]+$"to restrict matches to just the Arabic numerals 0 - 9.

请注意,"\d"将匹配[0-9]和其他数字字符类似的东方阿拉伯数字??????????。用于"^[0-9]+$"将匹配限制为仅阿拉伯数字 0 - 9。



If you need to include any numeric representations other than just digits (like decimal values for starters), then see @tchrist's comprehensive guide to parsing numbers with regular expressions.

如果您需要包含除数字以外的任何数字表示(例如初学者的十进制值),请参阅@tchrist使用正则表达式解析数字综合指南

回答by kasperjj

It is matching because it is finding "a match" not a match of the full string. You can fix this by changing your regexp to specifically look for the beginning and end of the string.

它是匹配的,因为它正在寻找“匹配”而不是完整字符串的匹配。您可以通过更改正则表达式来专门查找字符串的开头和结尾来解决此问题。

^\d+$

回答by Robert Gamble

Your regex will match anything that contains a number, you want to use anchors to match the whole string and then match one or more numbers:

您的正则表达式将匹配任何包含数字的内容,您希望使用锚点来匹配整个字符串,然后匹配一个或多个数字:

regex = new Regex("^[0-9]+$");

The ^will anchor the beginning of the string, the $will anchor the end of the string, and the +will match one or more of what precedes it (a number in this case).

^将锚定字符串的开始,$将锚定字符串的结尾,而+将匹配一个或多个(在这种情况下的数字)什么它之前。

回答by Mark Brackett

^\d+$, which is "start of string", "1 or more digits", "end of string" in English.

^\d+$,英文为“字符串的开始”、“1 位或多位数字”、“字符串的结尾”。

回答by James Selvakumar

If you want to extract only numbers from a string the pattern "\d+" should help.

如果您只想从字符串中提取数字,则模式 "\d+" 应该会有所帮助。

回答by Andy

If you need to tolerate decimal point and thousand marker

如果您需要容忍小数点和千位标记

var regex = new Regex(@"^-?[0-9][0-9,\.]+$");

You will need a "-", if the number can go negative.

如果数字可以变为负数,您将需要一个“-”。

回答by S.M.Mousavi

Another way: If you like to match international numbers such as Persian or Arabic, so you can use following expression:

另一种方式:如果您喜欢匹配波斯语或阿拉伯语等国际号码,则可以使用以下表达式:

Regex = new Regex(@"^[\p{N}]+$");

To match literal period character use:

要匹配文字句点字符,请使用:

Regex = new Regex(@"^[\p{N}\.]+$");

回答by Raz Megrelidze

Perhaps my method will help you.

或许我的方法可以帮到你。

    public static bool IsNumber(string s)
    {
        return s.All(char.IsDigit);
    }

回答by ultraklon

This works with integers and decimal numbers. It doesn't match if the number has the coma thousand separator ,

这适用于整数和十进制数。如果数字有逗号千位分隔符,则不匹配,

"^-?\d*(\.\d+)?$"

some strings that matches with this:

一些与此匹配的字符串:

894
923.21
76876876
.32
-894
-923.21
-76876876
-.32

some strings that doesn't:

一些没有的字符串:

hello
9bye
hello9bye
888,323
5,434.3
-8,336.09
87078.

回答by fnc12

Sorry for ugly formatting. For any number of digits:

对不起,丑陋的格式。对于任意数量的数字:

[0-9]*

For one or more digit:

对于一位或多位数字:

[0-9]+