I have got a text string like this:
我有一个这样的文本字符串:
test1test
I want to check if it contains at least one digit using a regex.
我想用正则表达式检查它是否包含至少一个数字。
What would this regex look like?
这个regex是什么样子的?
7 个解决方案
#1
58
I'm surprised nobody has mentioned the simplest version:
我很惊讶没有人提到最简单的版本:
\d
This will match any digit. If your regular expression engine is Unicode-aware, this means it will match anything that's defined as a digit in any language, not just the Arabic numerals 0-9.
这将匹配任何数字。如果正则表达式引擎具有unicode感知,这意味着它将匹配任何语言中定义为数字的任何东西,而不仅仅是阿拉伯数字0-9。
There's no need to put it in [
square brackets]
to define it as a character class, as one of the other answers did; \d
works fine by itself.
不需要把它放在[方括号]中来定义为字符类,就像其他答案中的一个那样;\d本身工作正常。
Since it's not anchored with ^
or $
, it will match any subset of the string, so if the string contains at least one digit, this will match.
因为它不是固定^和$,它将匹配任何子集的字符串,如果字符串包含至少一个数字,这将匹配。
And there's no need for the added complexity of +
, since the goal is just to determine whether there's at least one digit. If there's at least one digit, this will match; and it will do so with a minimum of overhead.
而且,没有必要增加+的复杂性,因为目标只是确定是否有至少一个数字。如果有至少一个数字,这将匹配;这样做只需要最少的开销。
#2
12
The regular expression you are looking for is simply this:
您正在寻找的正则表达式简单如下:
[0-9]
You do not mention what language you are using. If your regular expression evaluator forces REs to be anchored, you need this:
你没有提到你在用什么语言。如果正则表达式求值器强制将REs固定,则需要:
.*[0-9].*
Some RE engines (modern ones!) also allow you to write the first as \d
(mnemonically: digit) and the second would then become .*\d.*
.
一些RE engine(现代的)也允许您将第一个写成\d(记忆:digit),然后第二个就会变成。*\d.*。
#3
9
you could use look-ahead assertion for this:
您可以使用前面的断言:
^(?=.*\d).+$
#4
8
In Java:
在Java中:
public boolean containsNumber(String string)
{
return string.matches(".*\\d+.*");
}
#5
1
This:
这样的:
\d+
should work
应该工作
Edit, no clue why I added the "+", without it works just as fine.
编辑,不知道为什么我添加了“+”,没有它也一样好。
\d
#7
0
In perl:
在perl中:
if($testString =~ /\d/)
{
print "This string contains at least one digit"
}
where \d
matches to a digit.
哪里的\d匹配一个数字。
#1
58
I'm surprised nobody has mentioned the simplest version:
我很惊讶没有人提到最简单的版本:
\d
This will match any digit. If your regular expression engine is Unicode-aware, this means it will match anything that's defined as a digit in any language, not just the Arabic numerals 0-9.
这将匹配任何数字。如果正则表达式引擎具有unicode感知,这意味着它将匹配任何语言中定义为数字的任何东西,而不仅仅是阿拉伯数字0-9。
There's no need to put it in [
square brackets]
to define it as a character class, as one of the other answers did; \d
works fine by itself.
不需要把它放在[方括号]中来定义为字符类,就像其他答案中的一个那样;\d本身工作正常。
Since it's not anchored with ^
or $
, it will match any subset of the string, so if the string contains at least one digit, this will match.
因为它不是固定^和$,它将匹配任何子集的字符串,如果字符串包含至少一个数字,这将匹配。
And there's no need for the added complexity of +
, since the goal is just to determine whether there's at least one digit. If there's at least one digit, this will match; and it will do so with a minimum of overhead.
而且,没有必要增加+的复杂性,因为目标只是确定是否有至少一个数字。如果有至少一个数字,这将匹配;这样做只需要最少的开销。
#2
12
The regular expression you are looking for is simply this:
您正在寻找的正则表达式简单如下:
[0-9]
You do not mention what language you are using. If your regular expression evaluator forces REs to be anchored, you need this:
你没有提到你在用什么语言。如果正则表达式求值器强制将REs固定,则需要:
.*[0-9].*
Some RE engines (modern ones!) also allow you to write the first as \d
(mnemonically: digit) and the second would then become .*\d.*
.
一些RE engine(现代的)也允许您将第一个写成\d(记忆:digit),然后第二个就会变成。*\d.*。
#3
9
you could use look-ahead assertion for this:
您可以使用前面的断言:
^(?=.*\d).+$
#4
8
In Java:
在Java中:
public boolean containsNumber(String string)
{
return string.matches(".*\\d+.*");
}
#5
1
This:
这样的:
\d+
should work
应该工作
Edit, no clue why I added the "+", without it works just as fine.
编辑,不知道为什么我添加了“+”,没有它也一样好。
\d
#6
#7
0
In perl:
在perl中:
if($testString =~ /\d/)
{
print "This string contains at least one digit"
}
where \d
matches to a digit.
哪里的\d匹配一个数字。