如何使用正则表达式匹配 Ruby 中包含特殊字符的重复模式?
How to match repeating patterns containing special characters in Ruby using regex?
基本上,我正在尝试使用正则表达式来匹配 Ruby 中包含特殊字符的重复模式。如果我得到一个模式重复但不是动态的次数,我就能够做到这一点。我要匹配的示例字符串是:
Draw a square that is {{coords.width}} pixels wide by {{coords.height}} pixels tall.
这可以通过使用
轻松完成
arr = value.scan(/\{\{(\w+?\.\w+?)\}\}/).flatten
arr 在我 运行 this
之后看起来像这样
["coords.width", "coords.height"]
但是我如何编写一个可以匹配的正则表达式,以防这种模式任意遵循,例如
Draw a square that is {{shape.rectangle.coords.width}} pixels wide by {{shape.rectangle.coords.height}} pixels tall.
同时在以下情况下也匹配(没有“.”)
Draw a square that is {{width}} pixels wide by {{height}} pixels tall.
(/\{\{(.*?)\}\}/)
这成功了。它匹配 {{}} 中的任何内容,但我始终可以在提取 occurrences/patterns
时验证结构
(\{+\S+)
上面的模式可以实现您的目标。它匹配附件中的所有非 space 字符。
可以匹配正则表达式
r = /(?<=\{\{)[a-z]+(?:\.[a-z]+)*(?=\}\})/
Rubular demo / PCRE demo at regex 101.com
我包含了 PCRE 演示,因为 regex101.com 提供了对正则表达式每个元素的详细解释(将光标悬停)。
例如,
str = "Draw a square {{coords.width}} wide by {{coords.height}} " +
"tall by {{coords deep}} deep"
str.scan(r)
#=> ["coords.width", "coords.height"]
请注意 "coords deep"
不匹配,因为它没有(我假设的)有效形式。另请注意,我不必展平 scan
中的 return 值,因为正则表达式没有捕获组。
我们可以自由间距模式编写正则表达式,使其自记录。
/
(?<= # begin a positive lookbehind
\{\{ # match 1 or more lower case letters
) # end the positive lookbehind
[a-z]+ # match 1 or more lower case letters
(?: # begin a non-capture group
\. # match a period
[a-z]+ # match 1 or more lower case letters
) # end the non-capture group
* # execute the non-capture group zero or more times
(?= # begin a positive lookahead
\}\} # match '}}'
) # end positive lookahead
/x # free-spacing regex definition mode
基本上,我正在尝试使用正则表达式来匹配 Ruby 中包含特殊字符的重复模式。如果我得到一个模式重复但不是动态的次数,我就能够做到这一点。我要匹配的示例字符串是:
Draw a square that is {{coords.width}} pixels wide by {{coords.height}} pixels tall.
这可以通过使用
轻松完成arr = value.scan(/\{\{(\w+?\.\w+?)\}\}/).flatten
arr 在我 运行 this
之后看起来像这样["coords.width", "coords.height"]
但是我如何编写一个可以匹配的正则表达式,以防这种模式任意遵循,例如
Draw a square that is {{shape.rectangle.coords.width}} pixels wide by {{shape.rectangle.coords.height}} pixels tall.
同时在以下情况下也匹配(没有“.”)
Draw a square that is {{width}} pixels wide by {{height}} pixels tall.
(/\{\{(.*?)\}\}/)
这成功了。它匹配 {{}} 中的任何内容,但我始终可以在提取 occurrences/patterns
时验证结构(\{+\S+)
上面的模式可以实现您的目标。它匹配附件中的所有非 space 字符。
可以匹配正则表达式
r = /(?<=\{\{)[a-z]+(?:\.[a-z]+)*(?=\}\})/
Rubular demo / PCRE demo at regex 101.com
我包含了 PCRE 演示,因为 regex101.com 提供了对正则表达式每个元素的详细解释(将光标悬停)。
例如,
str = "Draw a square {{coords.width}} wide by {{coords.height}} " +
"tall by {{coords deep}} deep"
str.scan(r)
#=> ["coords.width", "coords.height"]
请注意 "coords deep"
不匹配,因为它没有(我假设的)有效形式。另请注意,我不必展平 scan
中的 return 值,因为正则表达式没有捕获组。
我们可以自由间距模式编写正则表达式,使其自记录。
/
(?<= # begin a positive lookbehind
\{\{ # match 1 or more lower case letters
) # end the positive lookbehind
[a-z]+ # match 1 or more lower case letters
(?: # begin a non-capture group
\. # match a period
[a-z]+ # match 1 or more lower case letters
) # end the non-capture group
* # execute the non-capture group zero or more times
(?= # begin a positive lookahead
\}\} # match '}}'
) # end positive lookahead
/x # free-spacing regex definition mode