正则表达式在双花括号之间得到任何东西 [英] regex get anything between double curly braces

查看:417
本文介绍了正则表达式在双花括号之间得到任何东西的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取.NET中双花括号之间的文本。
我知道了:

I need to get the text between double curly braces in .NET. I got this:

\{{([^}]+)\}}

但是,它还包括双花括号。
是否有排除它们的方法?

However, it also includes the double curly braces. Is there a way to exclude them?

推荐答案

我不是regex的大师,但这应该可以工作并回答有关在此之前和之后专门选择元素的问题。试试:

I'm no master of regex, but this ought to work and answers your question about exclusively selecting elements before and after. Try:

(?<=\{\{).*(?=\}\})

如果您想对此进行解释,我将向您介绍:

(?< = \ {\ {)正向隐藏 的开始 *,它在您要查找的内容之后匹配一个组,但不将其包含在结果中。这基本上是您要求的第一部分。 \ {\ {)是您实际上正在寻找的转义字符。

If you'd like an explanation as to why this works, I'll walk you through it:
(?<=\{\{) is the start of a positive lookbehind*, which matches a group after what you're looking for without including it in the result. This is basically the first part of what you asked for. The \{\{) are the escaped characters you're actually looking for.

。* 表示您想选择任意次数,直到得到为止到组(?= \} \}),这是正向超前,本质上是您要查找的第二部分。这将选择该选择器后面的所有内容,即两个转义的花括号,而不选择花括号本身。

.* is saying you'd like to select anything any amount of times until you get to the group (?=\}\}) which is a positive lookahead, essentially the second part of what you're looking for. This selects everything behind that selector, the two escaped end curly braces, without selecting the braces themselves.

*注意! javascript 支持正向查找

*note! Positive lookbehinds are not supported in javascript.

这篇关于正则表达式在双花括号之间得到任何东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆