Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace single quote with double quote with Regex

Tags:

java

regex

I have an app that received a malformed JSON string like this:

{'username' : 'xirby'}

I need to replaced the single quotes ' with double quoates "

With these rule (I think):

  • A single quote comes after a { with one or more spaces
  • Comes before one or more spaces and :
  • Comes after a : with one more spaces
  • Comes before one or more spaces and }

So this String {'username' : 'xirby'} or

{  'username' : 'xirby'  }

Would be transformed to:

{"username" : "xirby"}

Update:

Also a possible malformed JSON String:

{  'message' : 'there's not much to say'  }

In this example the single quote inside the message value should not be replaced.

like image 359
quarks Avatar asked Mar 23 '23 15:03

quarks


1 Answers

Try this regex:

\s*\'\s*

and a call to Replace with " will do the job. Look at here.

like image 105
NeverHopeless Avatar answered Apr 10 '23 21:04

NeverHopeless