Ruby正则表达式拆分换行符,空格或逗号

Bob*_*son 2 ruby regex

输入:

     f = ["happy days",
         "happy\ndays",
         "happy,days",
         "happy, days"]

     patt = /some_regex/
Run Code Online (Sandbox Code Playgroud)

在每个分割字符串后面我期望的输出fpatt为["快乐","天".

非常感谢!

Mar*_*ers 12

试试这个作为你的模式:

/\W+/
Run Code Online (Sandbox Code Playgroud)

它匹配任意数量的非单词字符.

示例代码:

result = f.map{|s| s.split(/\W+/) }
Run Code Online (Sandbox Code Playgroud)

看到它在线工作:ideone


小智 5

要么 /[\n ,]+/