简体   繁体   中英

MVC C# single regex to validate curly braces and angle brackets

I am trying to make a spinner for one of my projects, and I have a text area and I want to validate text if curly braces are closed and text don't have angle brackets.

So far I am able to check if text has any angle brackets present with [^<>]*

I have this string {hello|hi|hey} arbaz, {how are you?|how you doing} and i want to validate if curly braces is closed correctly. for example string can't be

{{hello|hi|hey}} arbaz, {{how are you?|how you doing}}

OR

{{hello|hi|hey} arbaz, {how are you?|how you doing}}

it can't be wrap in curly brackets.

\\{{?>\\{{?<c>}|[^{}]+|\\}{?<-c>}}*{?{c}{?!}}\\} is my regex so far.

I need a regex which can validate with curly braces and don't allow the angle brackets. Please forgive me for my bad English. Thanks

You want to match a string that contains either chars other than { and } OR {...} substrings and none of these can also match < and > chars.

You may use

^(?:[^<>{}]|{[^<>{}]*})*$

See the regex demo

It matches:

  • ^ - start of string
  • (?: - start of a non-capturing group
    • [^<>{}] - any char but < , > , { and }
    • | - or
    • { - a {
    • [^<>{}]* - 0+ chars other than < , > , { and }
    • } - a } char
  • )* - repeat the group pattern 0+ times
  • $ - end of string.

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