Search code examples
c#regexstringsmalltalkpharo

Regex: Matching all words in a phrase


Is this possible?

For a sentence such as hello how are you, I'd like my regular expression to return hello how are you. It only ever returns just hello and not the other words.

My Regex:

[A-Za-z]*

Any help is greatly appreciated. Thanks! If it matters, I'm using Pharo Smalltalk. I've been testing in too.


Solution

  • If you only need split the sentence by spaces this can be done using string.Split() method:

    var s = "hello how are you";
    var words = s.Split();
    

    If you want to use regular expressions:

    var s = "hello how are you";
    var regex = "\\w+";
    var words = Regex.Matches(s, regex).Cast<Match>().Select(m => m.Value);