Prabhakaran Parthipan
Prabhakaran Parthipan

Reputation: 4273

How to find out if a string contains digits followed by alphabet characters?

How can I find out whether a given string contains at least one number followed by alphabets using Regex in c#?

For example :

var strInput="Test123Test";

The function should return a bool value.

Upvotes: 0

Views: 252

Answers (3)

csharp newbie
csharp newbie

Reputation: 638

Try this:

if(Regex.IsMatch(strInput, @"[0-9][A-Za-z]"))
...

Upvotes: 0

Pratik Singhal
Pratik Singhal

Reputation: 6492

If you want to match 123 only then:-

Match m = Regex.Match("Test123Test", @"(\p{L}+)(\d+)") ; 
string result = m.Groups[2].Value  

If you want to get the bool value then do this:-

 Console.WriteLine(!String.IsNullOrEmtpty(result))) ;   

or simple use:-

  Regex.IsMatch("Test123Test", @"\p{L}+\d+") ; 

Upvotes: 0

Tim Pietzcker
Tim Pietzcker

Reputation: 336158

result = Regex.IsMatch(subjectString, @"\d\p{L}");

will return True for your sample string. Currently, this regex also considers non-ASCII digits and letters as valid matches, if you don't want that, use @"[0-9][A-Za-z])" instead.

Upvotes: 3

Related Questions