Change type of enclosure brackets with special conditions

Viewed 19

What I'm trying to achieve is changing square brackets [] to curly/brace brackets {}.

There are two conditions, some start with [", the others end with "]

There will not be any occurrences where both exist in same string. Haven't run across any yet.

BEFORE:

[Strained breathing]


["Wanna Give My Love"
by The Sons of Rainier]


[Mavrick blows a fart]


["Hallelujah"
by The Sons of Rainer]


[Victor over the phone]


[The Korgi's "Everybody's
Got To Learn Sometime"]


[Lola chuckles]


["It's Good"
by Jack Hammer]


[Uno Hype's "Leave"]

Here's what I would like as the end results

AFTER:

[Strained breathing]


{"Wanna Give My Love"
by The Sons of Rainier playing}


[Mavrick blows a fart]


{"Hallelujah"
by The Sons of Rainer}


[Victor over the phone]


{The Korgi's "Everybody's
Got To Learn Sometime"}


[Lola chuckles]


{"It's Good"
by Jack Hammer}


{Uno Hype's "Leave"}

Here are my attempts:

Find:   (?=\[")([\S\s]+?)\]
Replace: \{$1\}

Find:   (?=\[[A-Z])([\S\s]+?)\"]
Replace: \{$1\}

Find:   \["([A-Z][\S\s]+?)\]
Replace: \{$1\}

So frustrated that my light blub is still so dim in regards to regex.

Thanks in Advance

2 Answers

You could use this regex:

\[("[^]]+|[^]]+")\]

which matches a [ followed by either

  • a " and some number of non-] characters; or
  • some number of non-] characters followed by a "

and then followed by a ], and replace it with {\1}.

Regex demo on regex101

You can use

\[([^]["]*"[^][]*)]

Explanation

  • \[ Match [
  • ( Capture group 1
    • [^]["]* Optionally match any char except ] [ "
    • " Then match a single "
    • [^][]* Optionally match any char except ] [
  • ) Close group 1
  • ] Match ]

Regex demo

In the replacement use {\1}

enter image description here

Related