在正则表达式C中匹配{{string}} [英] matching {{string}} in regex c#

查看:52
本文介绍了在正则表达式C中匹配{{string}}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{{string}}

结果应为 string .

我是否必须同时使用大括号?

Do I have to escape both curly brackets?

推荐答案

不,实际上以下内容应该可以正常工作:

No, actually the following should work just fine:

"{{([^}]*)}}"

修改:正如 dtb 所指出的,以上表达式对于双括号中包含单个} 的字符串失败.为了处理这种情况,以下示例将做得更好:

As pointed out by dtb the expression above fails for a string containing a single } within the double brackets. To handle this case the following example would do a much better job:

"{{((?:}(?!})|[^}])*)}}"

但是,最简单的解决方案可能是:

Edit 2: The simplest solution however would probably be the following:

"{{(.*?)}}"

这篇关于在正则表达式C中匹配{{string}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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