0

During my searching, I would like a piece of advice :

I'd like to transform this, with replace method :

"labels": "\"label_name\": \"Webapp\","

to :

"labels": ["Webapp"]

in java. In fact, I'm embarrassed with "\" \".

If someone could me suggest me something.

Thanks.

0

1 Answer 1

1

replace can't use regex, but you can use replaceAll:

String input = "\"labels\": \"\\\"label_name\\\": \\\"Webapp\\\",\"";

String result = input.replaceAll(
    "(\"\\w+\")\\s*:[^:]+:\\s*\\\\\\\"(\\w+)\\\\\\\",\"", 
    "$1: \\[\"$2\"\\]");

System.out.println(result1); // "labels": ["Webapp"]
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks mates, It works like a charm ! Please if you have a little time, could you explain me the complete sense of the regex ? I am looking here now : ... . If I understand I will put the description.
@Ale Regex101 does a good job of explaining: regex101.com/r/UVqVYo/1 (see on the right). You just have to un-escape some things. The $1 and $2 are referencing the capture groups from the first argument.
Wow so cool .. I have just seen the website ! It is marvellous Thanks dude !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.