Regular expression to verify that string contains { }

regex

I need to write a regular expression to verify that a string contains { } but not { or }.Can someone shine some light on this please?

Thanks for all the help , here are some examples.

e.g.

valid : {abc}, as09{02}dd, {sdjafkl}sdjk, sfdsjakl,00{00}00, aaaaa{d}

invalid: {sdsf , sdfadf},sdf{Sdfs ,333}333

*********Update*******************

^[a-zA-Z0-9_-. ](?:{[a-zA-Z0-9_-.]+})?[a-zA-Z0-9_-. ]$ is what I need,thanks for all your help 🙂

Best Answer

/.*\{.*\}.*/

This would ensure that the string contains an opening curly bracket somewhere before a closing curly bracket, occurring anywhere in the string. However, it wouldn't be able to ensure that there's only one opening and closing curly bracket -- to do that, the .* patterns would have to be changed to something more restrictive.

If you want to experiment and test these regexes out, here's a good site.