Reputation: 783
In my Dot net Application I have to find all the words which contain alphabets and numeric values,I tried to find but fail to find proper answer
I have (?=.\d)\w+
regex but it returns 45 as valid. Below is my sample list in which bold are right words and other are false words
I cannot put [a-zA-Z] because my text may contain other than English language ex. โครงการสาวอีสานก็5000บาท.
Upvotes: 1
Views: 122
Reputation: 11228
This also works:
string pattern = @"^(?=\D*\d)(?=\d*\D)\w+$";
To check:
var inputs = new List<string> { "madan45", "45madan", "mad45an", "Madan", "45" };
foreach (var str in inputs)
Console.WriteLine($"{str} - {Regex.IsMatch(str, pattern)}");
output:
// madan45 - True
// 45madan - True
// mad45an - True
// Madan - False
// 45 - False
Upvotes: 1
Reputation: 67968
(?:^|(?<=\s))(?=\S*\d)(?=\S*(?!\s)\D)\S+(?=\s|$)
Try this.See demo.
https://regex101.com/r/cJ6zQ3/15
Upvotes: 2
Reputation: 1212
^(?!(\d+|[A-z]+)$)\w+$
(\d+|[A-z]+)$
:all letter or all digit.
(?!(\d+|[A-z]+)$)
:Negative Lookahead - Assert that it is impossible to match all letter or all digit.
Upvotes: 2
Reputation: 6511
all the words which contain alphabets and numeric values
fail to find proper answer I have
(?=.\d)\w+
You were close. The lookahead should match any word character before the digit to meet the numeric condition. And then, in the main pattern, require a letter:
Regex:
(?=\w*\d)\d*[A-Za-z]\w*
\w*
Any word character: [A-Za-z0-9_]
\d
Requires 1 digit.\d*
Any digits preceding a letter.[A-Za-z]
Requires 1 letter.\w*
Rest of the wordUpvotes: 1
Reputation: 1073
(?:\d+[a-z]|[a-z]+\d)[a-z\d]*
This Reg Ex should work for your requirements.
Upvotes: 1