将字符串与可变左/右分隔符匹配

时间:2022-08-22 12:11:22

The problem is quite easy. I would like to match anything between some strings at the beginning and some strings at the end. Strings at the end should match appropriate strings at the beginning.

问题很简单。我想在开头的一些字符串和最后的一些字符串之间匹配任何内容。最后的字符串应该在开头匹配适当的字符串。

Let's assume that I want to match everything that is between [ and ] or { and }.

我们假设我想匹配[和]或{和}之间的所有内容。

The first regular expression that could be used is:

可以使用的第一个正则表达式是:

/[{\[](.*)[}\]]/gmU

/[{\[](.*)[}\]]/gmU

however there is one problem with it. When subject is:

但是它有一个问题。主题是:

{aa} werirweiu [ab] wrewre [ac}

{aa} werirweiu [ab] wrewre [ac}

also [ac} is matched but it shouldn't.

也是[ac}匹配但不应该匹配。

It can be easily changed into:

它可以很容易地变成:

/\[(.*)\]|\{(.*)\}/gmU

/\[(.*)\]|\{(.*)\}/gmU

and the problem is solved.

问题解决了。

But what in case if (.*) were much more complicated and beginnings and ends would be for example 10 and they also would be a bit more complicated (not one character but many)? Then using above rule the whole (.*) should be repeated 10 times and it would be illegible.

但是,如果(。*)更复杂,开始和结束将是例如10,它们也会更复杂(不是一个字符,但很多)?然后使用上面的规则,整个(。*)应该重复10次,这将是难以辨认的。

Is there any way to match ends with beginnings? For example I would like to use syntax similar to

有什么办法可以将结尾与开头相匹配吗?例如,我想使用类似的语法

/(aa|bb)(.*)(cc|ddd)/gmU to tell that match must begin with aa and ends with cc or begin with bb and ends with ddd and match in subject aaxx1cc bbxx2ddd aaxx3ddd bbxx4cc only strings xx1 and xx2 without repeating (.*) many times in that regular expression and remembering there might be more than 2 as in above examples beginnings and endings.

/(aa|bb)(.*)(cc|ddd)/gmU告诉那场比赛必须以aa开头并以cc开头或以bb开头并以ddd结束并在主题中匹配aaxx1cc bbxx2ddd aaxx3ddd bbxx4cc只有字符串xx1和xx2在正则表达式中多次重复(。*)并且记住可能存在多于2个,如上面的示例的开头和结尾。

1 个解决方案

#1


7  

Use a Conditional

In my view, this is a very nice place to use conditionals. This regex will work:

在我看来,这是一个使用条件的好地方。这个正则表达式将起作用:

(?:(\[)|({)).*?(?(1)\])(?(2)})

See what matches and fails in the Regex Demo.

在Regex演示中查看匹配和失败的内容。

Other Kinds of Delimiters

其他种类的分隔符

This is easy to expand: for instance, the following pattern will match strings delimited between START and END, or between <-- and -->, or between ==: and :==

这很容易扩展:例如,以下模式将匹配START和END之间或< - 和 - >之间或==:和之间分隔的字符串:==

(?:(START)|(<--)|(==:)).*?(?(1)END)(?(2)-->)(?(3):==)

See the Regex Demo.

请参阅正则表达式演示。

Explanation

说明

  • The non-capture group (?:(\[)|({)) matches the opening delimiter, i.e. either
  • 非捕获组(?:( \ [)|({))与开始定界符匹配,即
  • [ which (\[) captures to Group 1
  • [哪个(\ [)捕获到第1组
  • OR |
  • 或者|
  • { which ({) captures to Group 2
  • {which({)捕获到第2组
  • .*? lazily matches up to a point where...
  • 。*?懒洋洋地匹配到......
  • (?(1)\]) if Group 1 is set, we match ]
  • (?(1)\])如果组1设置,我们匹配]
  • (?(2)}) if Group 2 is set, we match }
  • (?(2)})如果设置了第2组,我们匹配}

Reference

参考

#1


7  

Use a Conditional

In my view, this is a very nice place to use conditionals. This regex will work:

在我看来,这是一个使用条件的好地方。这个正则表达式将起作用:

(?:(\[)|({)).*?(?(1)\])(?(2)})

See what matches and fails in the Regex Demo.

在Regex演示中查看匹配和失败的内容。

Other Kinds of Delimiters

其他种类的分隔符

This is easy to expand: for instance, the following pattern will match strings delimited between START and END, or between <-- and -->, or between ==: and :==

这很容易扩展:例如,以下模式将匹配START和END之间或< - 和 - >之间或==:和之间分隔的字符串:==

(?:(START)|(<--)|(==:)).*?(?(1)END)(?(2)-->)(?(3):==)

See the Regex Demo.

请参阅正则表达式演示。

Explanation

说明

  • The non-capture group (?:(\[)|({)) matches the opening delimiter, i.e. either
  • 非捕获组(?:( \ [)|({))与开始定界符匹配,即
  • [ which (\[) captures to Group 1
  • [哪个(\ [)捕获到第1组
  • OR |
  • 或者|
  • { which ({) captures to Group 2
  • {which({)捕获到第2组
  • .*? lazily matches up to a point where...
  • 。*?懒洋洋地匹配到......
  • (?(1)\]) if Group 1 is set, we match ]
  • (?(1)\])如果组1设置,我们匹配]
  • (?(2)}) if Group 2 is set, we match }
  • (?(2)})如果设置了第2组,我们匹配}

Reference

参考