简体   繁体   English

如何使用正则表达式匹配 Ruby 中包含特殊字符的重复模式?

[英]How to match repeating patterns containing special characters in Ruby using regex?

Basically, I am trying to use regex to match repeating patterns containing special characters in Ruby.基本上,我试图使用正则表达式来匹配 Ruby 中包含特殊字符的重复模式。 I've been able to do this if I am given the number of times a pattern repeats but not dynamically.如果我得到模式重复但不是动态的次数,我就能够做到这一点。 An example string I am looking to match is:我希望匹配的示例字符串是:

Draw a square that is {{coords.width}} pixels wide by {{coords.height}} pixels tall.

This can be easily done by using这可以通过使用轻松完成

arr = value.scan(/\\{\\{(\\w+?\\.\\w+?)\\}\\}/).flatten

arr looks like this after I run this我运行这个后 arr 看起来像这样

["coords.width", "coords.height"]

But how do I write a regex which can match in case this pattern follows arbitrarily, for example但是我如何编写一个可以匹配的正则表达式,以防这种模式任意遵循,例如

Draw a square that is {{shape.rectangle.coords.width}} pixels wide by {{shape.rectangle.coords.height}} pixels tall.

while also matching in case of the following(no ".")同时在以下情况下也匹配(没有“。”)

Draw a square that is {{width}} pixels wide by {{height}} pixels tall.

You can match the regular expression您可以匹配正则表达式

r = /(?<=\{\{)[a-z]+(?:\.[a-z]+)*(?=\}\})/

Rubular demo / PCRE demo at regex 101.com regex 101.com 上的Rubular 演示/ PCRE 演示

I've included the PCRE demo because regex101.com provides a detailed explanation of each element of the regex (hover the cursor).我已经包含了 PCRE 演示,因为 regex101.com 提供了正则表达式的每个元素的详细解释(悬停光标)。

For example,例如,

str = "Draw a square {{coords.width}} wide by {{coords.height}} " +
      "tall by {{coords deep}} deep"
str.scan(r)
  #=> ["coords.width", "coords.height"]

Notice that "coords deep" does not match because it does not have (what I have assumed is) a valid form.请注意, "coords deep"不匹配,因为它没有(我假设的)有效形式。 Notice also that I did not have to flatten the return value from scan because the regex has no capture groups.另请注意,由于正则表达式没有捕获组,因此我不必展平scan的返回值。

We can write the regular expression in free-spacing mode to make it self-documenting.我们可以以自由间距模式编写正则表达式以使其自记录。

/
(?<=      # 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

(/\\{\\{(.*?)\\}\\}/)

This did the trick.这成功了。 It matches anything that's within {{}} but I can always validate the structure upon extracting the occurrences/patterns它匹配 {{}} 内的任何内容,但我始终可以在提取出现次数/模式时验证结构

(\\{+\\S+)

The pattern above would achieve your goals.上面的模式将实现您的目标。 It matches all non space characters within the enclosure.它匹配外壳内的所有非空格字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM