Daredevi1 Daredevi1 - 1 month ago 5
Java Question

Java regex to remove everything between character 'i' and ':'

I have the following String

"{TEXT=9201i3:9830i22:90400i11:92710i7:94500|HELLO_UK}"


and I need to do the following:


  1. Remove everything between
    i
    and
    :
    and replace it with
    ,

  2. Remove
    {TEXT=

  3. Remove
    |HELLO_UK}



I want to end up with
"9201,9830,90400,92710,94500"


I've had an attempt at it but it doesn't quite do what I want. The following is what I've written:

numbers = numbers.replaceAll("\\D.*?:", ",");


I get the following result:
",9830,90400,92710,94500|HELLO_UK}"


Notice how it has removed the first set of numbers (9201).

Can anyone suggest the regex I should be using please?

Answer

Here you go:

String str = "{TEXT=9201i3:9830i22:90400i11:92710i7:94500|HELLO_UK}"
        .replaceAll("(\\{TEXT=|\\|HELLO_UK\\})", "")
        .replaceAll("i(.*?):", ",");

System.out.println(str);
Comments