Home > Software engineering >  Regex: string contains at least one set of curly brackets
Regex: string contains at least one set of curly brackets

Time:08-15

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.

CodePudding user response:

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]*?)\}"
  • Related