How to split this String in java such that I'll get the text occurring between the braces in a String array?
如何在java中拆分此String,以便我在String数组中的大括号之间发生文本?
GivenString = "(1,2,3,4,@) (a,s,3,4,5) (22,324,#$%) (123,3def,f34rf,4fe) (32)"
String [] array = GivenString.split("");
Output must be:
输出必须是:
array[0] = "1,2,3,4,@"
array[1] = "a,s,3,4,5"
array[2] = "22,324,#$%"
array[3] = "123,3def,f34rf,4fe"
array[4] = "32"
3 个解决方案
#1
7
You can try to use:
您可以尝试使用:
Matcher mtc = Pattern.compile("\\((.*?)\\)").matcher(yourString);
#2
2
The best solution is the answer by Rahul Tripathi, but your question said "How to split", so if you must use split()
(e.g. this is an assignment), then this regex will do:
最好的解决方案是Rahul Tripathi的答案,但你的问题是“如何分割”,所以如果你必须使用split()(例如这是一个赋值),那么这个正则表达式将会:
^\s*\(|\)\s*\(|\)\s*$
It says:
- Match the open-parenthesis at the beginning
- Match close-parenthesis followed by open-parenthesis
- Match the close-parenthesis at the end
在开头匹配开括号
匹配闭括号,然后是开括号
匹配最后的括号
All 3 allowing whitespace.
所有3个允许空格。
As a Java regex, that would mean:
作为Java正则表达式,这意味着:
str.split("^\\s*\\(|\\)\\s*\\(|\\)\\s*$")
See regex101 for demo.
有关演示,请参阅regex101。
The problem with using split()
is that the leading open-parenthesis causes a split before the first value, resulting in an empty value at the beginning:
使用split()的问题在于,前导开括号会在第一个值之前导致拆分,从而导致开头为空值:
array[0] = ""
array[1] = "1,2,3,4,@"
array[2] = "a,s,3,4,5"
array[3] = "22,324,#$%"
array[4] = "123,3def,f34rf,4fe"
array[5] = "32"
That is why Rahul's answer is better, because it won't see such an empty value.
这就是Rahul的答案更好的原因,因为它不会看到这样的空值。
#3
0
Usually, you would want to use the split() function as this is the easiest way to split a string into multiple arrays when the string is broken up by a key char.
通常,您可能希望使用split()函数,因为这是在字符串被键char分解时将字符串拆分为多个数组的最简单方法。
The main problem is that you need information inbetween two chars. The easiest way to solve this problem would to go through the string get ride of every instance of '('. This leaves the string looking like
主要问题是你需要两个字符之间的信息。解决这个问题的最简单的方法是通过'(''的每个实例的字符串获取。这使得字符串看起来像
String = "1,2,3,4,@) a,s,3,4,5) 22,324,#$%) 123,3def,f34rf,4fe) 32)"
String =“1,2,3,4,@)a,s,3,4,5)22,324,#$%)123,3def,f34rf,4fe)32)”
And this is perfect, as you can split by the char ')' and not worry about the other bracket interfering with the split. I suggest using the replace("","") where it replaces every instance of the first parameter with the second parameter (we can use "" to delete it).
这是完美的,因为你可以通过char')'分开,而不用担心其他支架干扰分裂。我建议使用replace(“”,“”),它用第二个参数替换第一个参数的每个实例(我们可以用“”来删除它)。
Here is some example code that may work :
以下是一些可能有效的示例代码:
String a = "(1,2,3,4,@) (a,s,3,4,5) (22,324,#$%) (123,3def,f34rf,4fe) (32)"
a = a.replace("(","");
//a is now equal to 1,2,3,4,@) a,s,3,4,5) 22,324,#$%) 123,3def,f34rf,4fe) 32)
String[] parts = a.split("\\)");
System.out.println(parts[0]); //this will print 1,2,3,4,@
I haven't tested it completely, so you may end up with unwanted spaces at the end of the strings you may need to get rid of!
我没有完全测试过,所以你可能需要摆脱字符串末尾的不需要的空格!
You can then loop through parts[] and it should have all of the required parts for you!
然后你可以遍历零件[],它应该包含所有必需的零件!
#1
7
You can try to use:
您可以尝试使用:
Matcher mtc = Pattern.compile("\\((.*?)\\)").matcher(yourString);
#2
2
The best solution is the answer by Rahul Tripathi, but your question said "How to split", so if you must use split()
(e.g. this is an assignment), then this regex will do:
最好的解决方案是Rahul Tripathi的答案,但你的问题是“如何分割”,所以如果你必须使用split()(例如这是一个赋值),那么这个正则表达式将会:
^\s*\(|\)\s*\(|\)\s*$
It says:
- Match the open-parenthesis at the beginning
- Match close-parenthesis followed by open-parenthesis
- Match the close-parenthesis at the end
在开头匹配开括号
匹配闭括号,然后是开括号
匹配最后的括号
All 3 allowing whitespace.
所有3个允许空格。
As a Java regex, that would mean:
作为Java正则表达式,这意味着:
str.split("^\\s*\\(|\\)\\s*\\(|\\)\\s*$")
See regex101 for demo.
有关演示,请参阅regex101。
The problem with using split()
is that the leading open-parenthesis causes a split before the first value, resulting in an empty value at the beginning:
使用split()的问题在于,前导开括号会在第一个值之前导致拆分,从而导致开头为空值:
array[0] = ""
array[1] = "1,2,3,4,@"
array[2] = "a,s,3,4,5"
array[3] = "22,324,#$%"
array[4] = "123,3def,f34rf,4fe"
array[5] = "32"
That is why Rahul's answer is better, because it won't see such an empty value.
这就是Rahul的答案更好的原因,因为它不会看到这样的空值。
#3
0
Usually, you would want to use the split() function as this is the easiest way to split a string into multiple arrays when the string is broken up by a key char.
通常,您可能希望使用split()函数,因为这是在字符串被键char分解时将字符串拆分为多个数组的最简单方法。
The main problem is that you need information inbetween two chars. The easiest way to solve this problem would to go through the string get ride of every instance of '('. This leaves the string looking like
主要问题是你需要两个字符之间的信息。解决这个问题的最简单的方法是通过'(''的每个实例的字符串获取。这使得字符串看起来像
String = "1,2,3,4,@) a,s,3,4,5) 22,324,#$%) 123,3def,f34rf,4fe) 32)"
String =“1,2,3,4,@)a,s,3,4,5)22,324,#$%)123,3def,f34rf,4fe)32)”
And this is perfect, as you can split by the char ')' and not worry about the other bracket interfering with the split. I suggest using the replace("","") where it replaces every instance of the first parameter with the second parameter (we can use "" to delete it).
这是完美的,因为你可以通过char')'分开,而不用担心其他支架干扰分裂。我建议使用replace(“”,“”),它用第二个参数替换第一个参数的每个实例(我们可以用“”来删除它)。
Here is some example code that may work :
以下是一些可能有效的示例代码:
String a = "(1,2,3,4,@) (a,s,3,4,5) (22,324,#$%) (123,3def,f34rf,4fe) (32)"
a = a.replace("(","");
//a is now equal to 1,2,3,4,@) a,s,3,4,5) 22,324,#$%) 123,3def,f34rf,4fe) 32)
String[] parts = a.split("\\)");
System.out.println(parts[0]); //this will print 1,2,3,4,@
I haven't tested it completely, so you may end up with unwanted spaces at the end of the strings you may need to get rid of!
我没有完全测试过,所以你可能需要摆脱字符串末尾的不需要的空格!
You can then loop through parts[] and it should have all of the required parts for you!
然后你可以遍历零件[],它应该包含所有必需的零件!