简体   繁体   English

正则表达式:字符串至少包含一组大括号

[英]Regex: string contains at least one set of curly brackets

I'm using Asp.Net.我正在使用 Asp.Net。

I have dynamic url strings that look like this: "/blog/{topic}/{id}"我有看起来像这样的动态 url 字符串: "/blog/{topic}/{id}"

In my instance, all I want to do is identify if the string contains at least one pair of matching curly brackets .在我的例子中,我要做的就是确定字符串是否包含至少一对匹配的花括号

I don't need to know how many or what's contained inside them.我不需要知道它们里面有多少或什么。

A valid match is:一个有效的匹配是:

  • left curly bracket左大括号
  • any number of letters (upper or lower)任意数量的字母(大写或小写)
  • right curly bracket右大括号

And, the sequence can appear anywhere in the string - start/middle/end.而且,序列可以出现在字符串中的任何位置 - 开始/中间/结束。

I've seen a number of similar questions/answers but the OP is generally looking for more detailed information about the contents, and thus the solution is often over-complicated for my needs.我已经看到了许多类似的问题/答案,但 OP 通常会寻找有关内容的更详细信息,因此解决方案通常过于复杂,无法满足我的需求。 I just want to know if the format exists .我只想知道格式是否存在

Any help appreciated.任何帮助表示赞赏。

Simple solution ,简单的解决方案

"\{(.*?)\}"

Match { followed by any character, except line terminators, zero to unlimited times, as few times as possible, followed by, } .匹配{后跟任何字符,行终止符除外,零到无限次,尽可能少,后跟}

However, this will match,但是,这将匹配,

"{abc{123}"

Alternatively, this solution limits the internal capture to just alphas,或者,此解决方案将内部捕获限制为仅 alpha,

"\{([a-zA-Z]*?)\}"

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

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