Regex to find words with specific character

后端 未结 3 1744
别那么骄傲
别那么骄傲 2021-01-29 02:27

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

相关标签:
3条回答
  • 2021-01-29 02:47

    Depending on how you want to handle punctuation such as hyphenation, consider using just \w*a\w*.

    FYI: \w matches a word character.

    0 讨论(0)
  • Try with below regular expression

    Regex regex = new Regex(@"[^\s]*[a][^\s]*");
    
    0 讨论(0)
  • 2021-01-29 03:02

    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

    0 讨论(0)
提交回复
热议问题