Java regex刪除字符“i”和“:”之間的所有內容

[英]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. 將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

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/11/01/7300eeffd764acbe41ae41eacad62ae8.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com