Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Split text into list of words contains only letters

I’m trying to split a text into a list of words that a word should contain only letters.
I tried this pattern [^a-zA-Z]+ like below:

var regex = new Regex(@"[^a-zA-Z]+", RegexOptions.Singleline | RegexOptions.Compiled);
var words = regex.Split(text).Where(w => !string.IsNullOrEmpty(w))

When the input is This is a t3st, it returns ["This", "is", "a", "t", "st"] but I’m looking for ["This", "is", "a"] result.

I implemented it in this way:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 var words = text.Split(' ', StringSplitOptions.RemoveEmptyEntries)
                 .Where(str => str.All(char.IsLetter))
                 .ToList();

However, looking for a regex solution.

>Solution :

I don’t know C# in particular, but this should work (should be matched against the string):

(?<=^| )          # Beginning of line or preceding space
  (?:             # 
    (?=[a-z])     #                        ...which is a letter
    .             # Match any character...
  )+              # 1 or more times
(?= |$)           # End of line or succeeding space
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading