正则表达式:匹配短语中的所有单词

Kin*_*Dan 2 c# regex string smalltalk pharo

这可能吗?

对于这样的句子hello how are you,我希望我的正则表达式能够返回hello how are you.它只返回hello而不是其他词.

我的正则表达式:

[A-Za-z]*
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏.谢谢!如果重要,我正在使用Pharo Smalltalk.我也一直在测试.

Lea*_*lia 6

同样在Pharo发送#substrings消息:

'Hello how are you' substrings
Run Code Online (Sandbox Code Playgroud)

并获得数组:

#('Hello' 'how' 'are' 'you').
Run Code Online (Sandbox Code Playgroud)


Art*_*aca 5

如果您只需要按空格拆分句子,则可以使用以下string.Split()方法完成:

var s = "hello how are you";
var words = s.Split();
Run Code Online (Sandbox Code Playgroud)

如果要使用正则表达式:

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


小智 5

你可以在这里找到关于Pharo的Regex的章节:

https://ci.inria.fr/pharo-contribution/view/Books/job/DeepIntoPharo/lastSuccessfulBuild/artifact/tmp/PBE2.pdf

我只想在你可以运行的空格上拆分字符串:

Character space split: 'My String To split'

您将获得包含所有单词的OrderedCollection.