I am using the following code:
String array[] = "aba;b12".split("[4\\,;\\Qab\\E开发者_运维问答]+");
for(String test : array)
System.out.println(test);
I expect:
a b12
However I get:
<blank line> 12
Edit: I cleaned up the code, sorry.
The whole Idea is to splt the code by 4, semicolon, ab, comma, and treat consecutive delimiter as one.
Edit: Sorry about all the confusion, I know the question was not as clear as it could have been.
Thanks
OK, I've finally worked out what you think \Q...\E
is doing. What I think you want is ([4\,;]|ab)+
(plus any necessary escaping if you're writing it as a string literal). Er, except that I don't understand why you've bothered escaping the comma.
I think \Qab\E isn't doing what you think it is, the regex will split by any character inside the square brackets so will split at a and b, try this instead:
Edit:
([4\,;]+)
I didn't quite read that correctly, that will give you
ab
b12
But the first part stands.
This
String array[] = "aba;b12".split("ab|,|;|4");
for(String test : array)
System.out.println(test);
outputs
<blank line>
a
b12
IYou can not split "aba;b12"
to get
a
b
12
You have to define what you wish to do and define the rule for it if you wish to use Regex. If its about parsing each letter alone and the numbers together you will have to do that manually or modify the original string so you can split it usign regex. If you give some background information on what you wish to achieve maby you will get some new Ideas.
String array[] = "aba;;b12".split("(\\Qab\\E+|[4;]+)+");
for(String test : array)
System.out.println(test);
精彩评论