简体   繁体   中英

Regex: string contains at least one set of curly brackets

I'm using Asp.Net.

I have dynamic url strings that look like this: "/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. 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,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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