Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.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,我正试图从字符串“the”中查找在字符“t”之前和字符“e”之后有空格的单词“”。我正在使用下面的正则表达式,但它只给了我一个单词“the”,而不是两个单词“the” s="the the the the" s.scan(/\sthe\s/) output - [" the "] 我希望表达式返回两个中间词“the”。为什么会发生这种情况。这里的问题是\s模式会占用空白。scan方法仅匹配不重叠的匹配,并且您期望的匹配是重叠的 您需要使用以获得重叠匹配: /(?<=\s)the(?=\s

我正试图从字符串“the”中查找在字符“t”之前和字符“e”之后有空格的单词
”。我正在使用下面的正则表达式,但它只给了我一个单词
“the”
,而不是两个单词
“the”

s="the the the the"
s.scan(/\sthe\s/)
output - [" the "]

我希望表达式返回两个中间词“the”。为什么会发生这种情况。

这里的问题是
\s
模式会占用空白。
scan
方法仅匹配不重叠的匹配,并且您期望的匹配是重叠的

您需要使用以获得重叠匹配:

/(?<=\s)the(?=\s)/
/(?”?