1

I have done some searching and would like advice on this problem:

I want to replace "labels":"Webapp" with "labels":["Webapp"]

I found the regex (\"labels\"\:\")+(([a-zA-Z]|\s|\-)+)+(\") with the following substitution "labels":["$2"]

I use the method replaceAll and the Talend editor.

I write output_row.json = output_row.json.replaceAll("(\"labels\"\:\")+(([a-zA-Z]|\s|\-)+)+(\")",""labels":["$2"]"); but It doesn't work.

Message détaillé: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \ )

Then I escaped the characters, I did:

output_row.json = output_row.json.replaceAll("(\\"labels\\"\:\\")+(([a-zA-Z]|\\s|\-)+)+(\\")","\"labels\":[\"$2\"]");

But It doesn't work yet.

Please could you help me?

Thanks.

0

1 Answer 1

2

Issues : don't escape - and : they are not special characters in regex

escape \s with \\s plus escape " as you did in your second example \"labels\":[\"$2\"]

Although you can use a more concise regex and combine your \\s , - inside character class []

You can use (\"labels\":\")+([a-zA-Z -]+)\

System.out.println("labels\":\"Webapp"
        .replaceAll("(\"labels\":\")+([a-zA-Z -]+)\""
                , "\"labels\":[\"$2\"]"));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.