开发者

How to do make this regular expression work? (Consecutive delimiter) [closed]

开发者 https://www.devze.com 2023-02-17 05:55 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号