简体   繁体   English

拆分非逗号分隔的字符串

[英]Splitting a non comma separated string

I have a query that concatenates the values of two columns and returns a string as shown below: 我有一个查询,将两列的值连接起来并返回一个字符串,如下所示:

423545(50),7568787(50),53654656,2021947(50),

Now i need to work on the individual numbers without the values in the brackets. 现在,我需要处理没有括号中的值的单个数字。 I could rerun another query to retrieve the values but i am thinking it is more efficient to split the existing string instead of making another database call. 我可以重新运行另一个查询以检索值,但是我认为拆分现有字符串比进行另一个数据库调用更为有效。

How can i split the above string to remove the values in brackets? 如何分割以上字符串以删除括号中的值? I want to store the numbers in an ArrayList. 我想将数字存储在ArrayList中。 The arrayList will contain 423545,7568787,53654656,2021947, (without the commas) arrayList将包含423545,7568787,53654656,2021947,(不包含逗号)

Note that not all columns will have the bracketed value - See third column There could be a comma at the end of the string. 请注意,并非所有列都具有括号值-请参阅第三列。字符串末尾可能会有逗号。

I can split it using String.split() if it was just with commas but i guess the above format requires a regular expression. 如果只是逗号,我可以使用String.split()拆分它,但是我猜上面的格式需要一个正则表达式。

String[] list = str.split("(\\(\\d+\\))?,");

看到这个演示

首先用正则表达式删除所有括号及其值,然后在结果上分割字符串。

使用String.replaceAll(regex,替换)查找所有出现的带括号的数字,并将其替换为“”,然后拆分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM