Reputation: 155
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.
Upvotes: 1
Views: 89
Reputation: 37414
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\"]"));
Upvotes: 2