Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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/5/ruby/21.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
Regex 如何匹配包含子字符串的模式_Regex_Ruby - Fatal编程技术网

Regex 如何匹配包含子字符串的模式

Regex 如何匹配包含子字符串的模式,regex,ruby,Regex,Ruby,我有以下案文: Testing some text {{ first_name | mask }} and another {{ city }} and again {{ state | mask_trail }} 我只想匹配包含管道的{{} 但当我做类似的事情时 text.scan(/({{.*?\|+.*?}})/) {{city}也得到匹配可以使用这个正则表达式 {{(?:(?!{{).)*\|(?:(?!{{).)*}} {{(?:(?!{{|}}).)*\|(?:(?!{{|}}

我有以下案文:

Testing some text {{ first_name | mask }} and another {{ city }} and again {{ state | mask_trail }}
我只想匹配包含管道的
{{}

但当我做类似的事情时

text.scan(/({{.*?\|+.*?}})/)

{{city}
也得到匹配

可以使用这个正则表达式

{{(?:(?!{{).)*\|(?:(?!{{).)*}}
{{(?:(?!{{|}}).)*\|(?:(?!{{|}}).)*}}
说明:

  • {{
    -匹配文字
    {{
  • (?:(?!{>)*
    -匹配除文本以外的任何文本
    {{
    ,也称为
  • \\\\\\\
    -匹配管道
    \\
    字符
  • (?:(?!{>)*
    -再次匹配除文本以外的任何文本
    {{
  • }
    -匹配文字
    }

另外,如果有这样的嵌套模式,并且您希望匹配最内部的模式,您可以使用这个正则表达式

{{(?:(?!{{).)*\|(?:(?!{{).)*}}
{{(?:(?!{{|}}).)*\|(?:(?!{{|}}).)*}}

看看这个Ruby代码

re = /{{(?:(?!{{|}}).)*\|(?:(?!{{|}}).)*}}/
str = 'Testing some text{{ {{ first_name | mask }} }} and another {{ city }} and again {{ state | mask_trail }}'

str.scan(re) do |match|
    puts match.to_s
end
产出

{{ first_name | mask }}
{{ state | mask_trail }}

我假设(部分是因为问题中的示例),匹配将排除以“{{”开头并以“}}”结尾、包含“|”且也包含“{{”的字符串。例如,两者都不包含

"{{ a | {{ b }}"
也不是

如果要匹配,可以使用@WiktorStribiżew在对问题的评论中建议的正则表达式

您可以按如下方式获得所需的匹配项

str = "Testing some text {{ first_name | mask }} and another {{ city }} " +
      "and again {{ state | mask_trail }}"

R = /
    {{         # match '{{'
    ((?!{{).)  # match one character not preceded by '{{'. Save in capture group 1
    *          # perform capture to group 1 zero or more times 
    \|         # match '|'
    \g<1>      # execute subroutine defined by capture group 1
    *          # perform preceding match zero or more times
    }}         # match '}}'
    /x         # free-spacing regex definition mode
我无法使用,因为我不想返回捕获组的内容。因此,我使用无块返回生成匹配项的枚举数,然后将其转换为匹配项数组。是关于正则表达式子例程或子表达式的讨论


我从@Pushpesh的答案中借用了
(?!{)。

你可以用一个更简单的
/{{(?:(?!{)。*?\\\\\\..*}/
为什么要疯狂地选择答案?你不想看到其他人吗?@enegunlimapps:很高兴我能帮上忙:)
str.gsub(R).to_a
  #=> ["{{ first_name | mask }}", "{{ state | mask_trail }}"]