Assuming there are no nested brackets, you can use the following regex to search for the text:
(?i)\((?=[^()]*(?:dj|mix|version|inch))([^()]+)\)
Note that the regex is case-insensitive, due to (?i)
in front - make it case-sensitive by removing it.
Check the syntax of your language to see if you can use r
prefix, e.g. r'literal_string'
, to specify literal string.
And use the following as replacement:
[$1]
You can include more keywords by adding keywords to (?:dj|mix|version|inch)
part, each keyword separated by |
. If the keyword contains (
, )
, [
, ]
, |
, .
, +
, ?
, *
, ^
, $
, \
, {
, }
you need to escape them (I'm 99% sure the list is exhaustive). An easier way to think about it is: if the keyword only contains space and alphanumeric (but note that the number of spaces is strict), you can add them into the regex without causing side-effect.
Dissecting the regex:
(?i)
: Case-insensitive mode\(
:(
is special character in regex, need to escape it by prepending\
.(?=[^()]*(?:dj|mix|version|inch))
: Positive look-ahead(?=pattern)
:[^()]*
: I need to check that the text is within bracket, not outside or in some other bracket, so I use a negated character class[^characters]
to avoid matching()
and spill outside the current bracket. The assumption I made also comes into play a bit here.(?:dj|mix|version|inch)
: A list of keywords, in a non-capturing group(?:pattern)
.|
means alternation.
([^()]+)
: The assumption about no nested bracket makes it easier to match all the characters inside the bracket. The text is captured for later replacement, since(pattern)
is capturing group, as opposed to(?:pattern)
.\)
:)
is special character in regex, need to escape it by prepending\
.