66

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?

zx8754
  • 52,746
  • 12
  • 114
  • 209
crauscher
  • 6,528
  • 14
  • 59
  • 85

9 Answers9

127

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.

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.

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.

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • 6
    Java's string.matches("\\d") returns false, where string="test1test". Suggestions? See here http://fiddle.re/vf9w6 – Watt Mar 31 '13 at 18:12
  • 2
    @Watt, if you play with that fiddle, you'll see that some methods *do* recognize the match -- `replaceFirst()` removes it, for example. It looks like [`matches` tries to match the entire string](http://stackoverflow.com/a/4450166/87399), implicitly adding `^` at the beginning of the pattern and `$` at the end. – Joe White Mar 31 '13 at 19:58
28

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:

.*[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.*.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
21

In Java:

public boolean containsNumber(String string)
{
    return string.matches(".*\\d+.*");
}  
17

you could use look-ahead assertion for this:

^(?=.*\d).+$
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
3

Another possible solution, in case you're looking for all the words in a given string, which contain a number

Pattern

\w*\d{1,}\w*
  • \w* - Matches 0 or more instances of [A-Za-z0-9_]
  • \d{1,} - Matches 1 or more instances of a number
  • \w* - Matches 0 or more instances of [A-Za-z0-9_]

The whole point in \w* is to allow not having a character in the beginning or at the end of a word. This enables capturing the first and last words in the text.

If the goal here is to only get the numbers without the words, you can omit both \w*.

Given the string

Hello world 1 4m very happy to be here, my name is W1lly W0nk4

Matches

1 4m W1lly W0nk4

Check this example in regexr - https://regexr.com/5ff7q

Meir Gabay
  • 2,870
  • 1
  • 24
  • 34
  • P.s- the expression `{1,}` can be replaced with `+` as it has the same effect. Though for beginners, I think that the former is easier to read – Meir Gabay Apr 26 '22 at 18:40
1

Ref this

SELECT * FROM product WHERE name REGEXP '[0-9]'
Community
  • 1
  • 1
Salil
  • 46,566
  • 21
  • 122
  • 156
1

This:

\d+

should work

Edit, no clue why I added the "+", without it works just as fine.

\d
Jurgen
  • 411
  • 10
  • 22
0

In perl:

if($testString =~ /\d/) 
{
    print "This string contains at least one digit"
}

where \d matches to a digit.

Kyra
  • 5,129
  • 5
  • 35
  • 55
0

If anybody falls here from google, this is how to check if string contains at least one digit in C#:

With Regex

if (Regex.IsMatch(myString, @"\d"))
{
  // do something
}

Info: necessary to add using System.Text.RegularExpressions

With linq:

if (myString.Any(c => Char.IsDigit(c)))
{
  // do something
}

Info: necessary to add using System.Linq

Vitox
  • 3,852
  • 29
  • 30