Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从这个字符串中提取单词!!一个**“两个**@@三个”;在Ruby中使用正则表达式_Ruby_Regex - Fatal编程技术网

如何从这个字符串中提取单词!!一个**“两个**@@三个”;在Ruby中使用正则表达式

如何从这个字符串中提取单词!!一个**“两个**@@三个”;在Ruby中使用正则表达式,ruby,regex,Ruby,Regex,在IRB中,我可以这样做: c=/(\b\w+\b)\w*(\b\w+\b)\w*(\b\w+\b)\w*/.match(“!!一***二*@@三@”)匹配 得到这个: =>匹配数据“一***二*@@三@@@1:“一”二:“二”三:“三” 但是假设我事先不知道单词的数量,我怎么还能从字符串中提取出所有单词呢”。例如,它可能是“!!”!!一个***两个*@@三个@“在一个实例中,但可能是”!!在另一个例子中是“五***六*” 谢谢 > " !!one** *two* @@three@@ ".

在IRB中,我可以这样做:
c=/(\b\w+\b)\w*(\b\w+\b)\w*(\b\w+\b)\w*/.match(“!!一***二*@@三@”)匹配

得到这个:
=>匹配数据“一***二*@@三@@@1:“一”二:“二”三:“三”

但是假设我事先不知道单词的数量,我怎么还能从字符串中提取出所有单词呢”。例如,它可能是“!!”!!一个***两个*@@三个@“在一个实例中,但可能是”!!在另一个例子中是“五***六*”

谢谢

> " !!one** *two* @@three@@ ".scan(/\w+/)
=> ["one", "two", "three"]
另外,
scan
可以在使用
()
的情况下返回数组数组

另外,
scan
可以在使用
()
的情况下返回数组数组

> "Our fifty users left 500 posts this month.".scan(/([a-z]+|\d+)\s+(posts|users)/i)
=> [["fifty", "users"], ["500", "posts"]]