简体   繁体   English

如何使用正则表达式匹配文档中的大括号?

[英]How to match braces in a document with regex?

I use VSCODE 1.41.1 to edit a MediaWiki formatted article with lots of MediaWiki template calls , such as:我使用 VSCODE 1.41.1 编辑带有大量MediaWiki 模板调用MediaWiki 格式文章,例如:

{{SOME_TEMPLATE|SOME_ARGUMNET}}

I want to match all {{ and }} brace pairs in that document.我想匹配该文档中的所有{{}}大括号对。

I Have tried:我试过了:

[{{], [}}]

Interesting for me to note that [{{] works, but only for matching a given single pair (yet I need both pairs in one operation).有趣的是我注意到[{{]有效,但仅用于匹配给定的一对(但我需要在一个操作中使用两个对)。

How to match braces in a document with regex?如何使用正则表达式匹配文档中的大括号?

在写这个问题时,我发现这个解决方案似乎工作正常:

[{{]|[}}]

[{{] is a character class that matches an opening brace or ... an opening brace! [{{]是一个字符类,它匹配一个左大括号或……一个左大括号! It is strictly equal to [{] or, depending on your regex flavor, a simple { .严格等于[{]或者,根据您的正则表达式风格,一个简单的{ Same for [}}] . [}}]

A direct matching alternative is: [{]{2}|[}][2}直接匹配的替代方法是: [{]{2}|[}][2}

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

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