Regex: Check if string contains at least one digit

后端 未结 9 1985
盖世英雄少女心
盖世英雄少女心 2020-12-09 07:40

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 reg

相关标签:
9条回答
  • 2020-12-09 08:14

    you could use look-ahead assertion for this:

    ^(?=.*\d).+$
    
    0 讨论(0)
  • 2020-12-09 08:15

    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

    0 讨论(0)
  • 2020-12-09 08:16

    Ref this

    SELECT * FROM product WHERE name REGEXP '[0-9]'
    
    0 讨论(0)
提交回复
热议问题