简体   繁体   中英

Regex C#, get everything between 2 characters but only when a specific character is in between and some others are missing

I have this regular expression, it's able to find everything inside square brackets ignoring the ones containing comma and other square brackets inside.

However is not finished, what I'm trying to achieve now, is to find only the ones that contains at least one '=' symbol inside the square brackets, if not, I'm not interested.

Current regex:

\[([^,\[\]]+?)\]

Input examples:

  • [Test=Test] -> Match: [Test=Test] and it's ok
  • [Test=Test;Test2=Test2] -> Match: [Test=Test;Test2=Test2] and it's ok
  • [Test,Test] -> No match and it's ok
  • [T[E]ST] -> Match: [E] and it's not ok (equal symbol is missing)
  • [T[E=S]T] -> Match: [E=S] and it's ok

Not sure if I'm in the correct direction because I'm already getting everything inside 2 characteres when some characteres are not inside.. but how to tell to do it only when a specific character is there..?

I hope someone can give me a hand, this regex is driving me crazy

Thanks

You may use

\[([^][,=]*=[^][,]*)]

See the regex demo .

Details

  • \\[ - a [ char
  • ([^][,=]*=[^][,]*) - Group 1:
    • [^][,=]* - 0 or more chars other than ] , [ , , and =
    • = - an equal sign
    • [^][,]* - 0 or more chars other than ] , [ and ,
  • ] - a ] char.

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