文字匹配模式

时间:2018-07-30 18:34:40

标签: regex

我有以下文字:

<h1>Hello world!</h1> <h2>What is {{ $slot }} Ipsum?</h2> <p><strong>Lorem Ipsum</strong> is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to {{$example }}make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially {{$category1 }} unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more {{ $Product2}} recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p> <p>&nbsp;</p><p>&nbsp;</p>

我尝试匹配以下模式:

{{ $slot }}
{{$example }}
{{ $Product2}}
{{$category1 }}

我当前的正则表达式如下^{{\s*$\w\s*}}

有人建议我在做什么错吗?

感谢您的答复!

2 个答案:

答案 0 :(得分:2)

使用表达式:

{{[^}]*}}
  • {{两个开括号。
  • [^}]*否定的字符集,除了}之外的任何字符。
  • }}两个右括号。

here试试。

答案 1 :(得分:2)

您的正则表达式与您的值不匹配,因为您声明了行^的开始,必须转义美元符号\$才能从字面上进行匹配,并且必须使用诸如示例+匹配一个或多个单词字符\w+

您可以使用:

{{\s*\$\w+\s*}}

Regex demo

说明:

  • {{字面上匹配
  • \s*匹配空白字符零次或多次
  • \$匹配一个美元符号
  • \s*匹配空白字符零次或多次
  • }}字面上匹配