omidh
omidh

Reputation: 2822

Regex expression for Changing close brackets to open brackets

I have a JSON text full of these:

  "order": "Commande",
  "@order":    }
    "description": "order word",
    "type": "texte"
  },

As you can see, There is an error in front of "@Order:" which } is used instead of {

How can I replace all of them with open bracket without changing }, at the end of objects? (I mean I need the regex expression to use it in search of my text editor)

^\s\}{1}\s didn't work

Upvotes: 0

Views: 107

Answers (3)

Bohemian
Bohemian

Reputation: 425013

Try this, which matches brackets at the ends of lines

(?m)\}$

See live demo.

Depending on your input, this might be enough.

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626802

Your ^\s\}\s pattern matches a whitespace, } and whitespace at the start of string. while the } you want to replace is at the end of string (or line).

You may consider using \}$, or \}(?=\s*$), or \}(?=\h*$) patterns to match } only at the end of a string/line (where $ is the end of string/line, (?=\s*$) matches a location that is immediately followed with any 0 or more whitespaces and then the end of a string/line, \h just only allows horizontal whitespaces).

However, if there is a colon before the } you need to replace you may consider a more sophisticated pattern like

(:\h*)\}(\h*$)
(:[^\S\r\n]*)\}([^\S\r\n]*$)

Replace with $1{$2 (or \1{\2 depending on the environment).

See the regex demo. Details:

  • (:\h*) - Capturing group 1 ($1): a colon and zero or more horizontal whitespaces
  • \} - a } char
  • (\h*$) - Group 2 ($2): zero or more horizontal whitespaces.

Note that [^\S\r\n] is a rough equivalent to \h, it matches any whitespace char but CR and LF chars.

Upvotes: 1

Alireza
Alireza

Reputation: 2123

Maybe this can help you

(?<=\"@\w+\":\s+)}

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

Upvotes: 1

Related Questions