sol
sol

Reputation: 93

Match all strings between brackets and with an uppercase

I want to match [abcDe] but not [abcde]. I used (?=\[([^\[\]]*)\]) to match all between brackets, but then I tried [A-Z]+(?=\[([^\[\]]*)\]) to only match between brackets when it contains an uppercase and it doesn't work.

Does anyone know how to solve this ?

Upvotes: 1

Views: 463

Answers (2)

The fourth bird
The fourth bird

Reputation: 163467

You might use

\[[^][A-Z]*[A-Z][^][]*]

The pattern matches:

  • \[ Match the opening square bracket
  • [^][A-Z]* Optionally repeat matching any char except [ ] or an uppercase char
  • [A-Z] Match a single uppercase char
  • [^][]* Optionally repeat matching any char except [ ]
  • ] Match the closing square bracket

Regex demo

Upvotes: 2

MonkeyZeus
MonkeyZeus

Reputation: 20747

You don't specify the regex flavor but this could work:

(?<=\[)[a-zA-Z]*[A-Z][a-zA-Z]*(?=\])
  • (?<=\[) - behind me is an opening bracket
  • [a-zA-Z]* - allow upper or lower case chars
  • [A-Z] - require one uppercase char
  • [a-zA-Z]* - allow upper or lower case chars
  • (?=\]) - ahead of me is a closing bracket

https://regex101.com/r/iz5Dtl/1

Upvotes: 1

Related Questions