Reputation: 4273
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
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
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