How to find all words which contain a specific letter in it?
For example, if my string is
This is a Station called South Yarra
then I
Depending on how you want to handle punctuation such as hyphenation, consider using just \w*a\w*
.
FYI: \w
matches a word character.
Try with below regular expression
Regex regex = new Regex(@"[^\s]*[a][^\s]*");
Solution without regex using Linq :
List<string> arr = s.Split(' ').Where(x => x.Contains('a')).ToList();
string.Split(' ') : It return array of strings that contains the substrings in this instance that are delimited by ' '
Enumerable.Where(predicate) : Filter sequence based on predicate
Enumerable.Contains() : Determines whether a sequence contains a specified element
POC: .net Fiddle