Java regex删除字符“i”和“:”之间的所有内容

时间:2021-06-03 07:28:30

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. 将i和之间的所有项删除:
  3. Remove {TEXT=
  4. 删除文本= {
  5. Remove |HELLO_UK}
  6. 删除| HELLO_UK }

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

最后我想说"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}"

我得到以下结果:“,9830,90400,92710,94500|HELLO_UK}”

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

注意它是如何删除第一个数字集(9201)的。

Can anyone suggest the regex I should be using please?

有人能建议我使用regex吗?

2 个解决方案

#1


2  

Here you go:

给你:

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

System.out.println(str);

#2


0  

You can also try following :

你也可以尝试以下方式:

String str = "{TEXT=9201i3:9830i22:90400i11:92710i7:94500|HELLO_UK}";
str.replaceAll("[(A-Z)*{|}_=]", "")
   .replaceAll("[i:]", ",");

System.out.println(str);

Output will be

输出将

9201,3,9830,22,90400,11,92710,7,94500

#1


2  

Here you go:

给你:

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

System.out.println(str);

#2


0  

You can also try following :

你也可以尝试以下方式:

String str = "{TEXT=9201i3:9830i22:90400i11:92710i7:94500|HELLO_UK}";
str.replaceAll("[(A-Z)*{|}_=]", "")
   .replaceAll("[i:]", ",");

System.out.println(str);

Output will be

输出将

9201,3,9830,22,90400,11,92710,7,94500