1

I want to capture all the consecutive groups in a binary string

1000011100001100111100001

should give me

1
0000
111
0000
11
00
1111
0000
1

I have made ([1?|0?]+) regex in my java application to group the consequential 1 or 0 in the string like 10000111000011. But when I run it in my code, there is nothing in the console printed:

String name ="10000111000011";
    regex("(\\[1?|0?]+)" ,name);

    public static void regex(String regex, String searchedString) {

        Pattern pattern = Pattern.compile(regex);
        Matcher regexMatcher = pattern.matcher(searchedString);
        while (regexMatcher.find()) 
            if (regexMatcher.group().length() > 0)
                System.out.println(regexMatcher.group());
    }

To avoid syntax error in the runtime of regex, I have changed the ([1?|0?]+) to the (\\[1?|0?]+)

Why there is no group based on regex?

4
  • Please explain why you downvoted this question? Commented Jan 18, 2017 at 11:30
  • i edited my question, I know. it just consider all as one group. But u shouldn't downvote me because of it :( Commented Jan 18, 2017 at 11:33
  • 1
    Next time please provide sample input and output from the start :) Commented Jan 18, 2017 at 11:43
  • Yes, sure. I just thought, the tester site accepts it, but i didn't noticed it accepted it as only one group Commented Jan 18, 2017 at 11:48

2 Answers 2

2

First - just as an explanation - your regex defines a character class ([ ... ]) that matches any of the characters 1, ?, | or 0 one or more times (+). I think you mean to have ( ... ) in it, among other things, which would make the | an alternation lazy matching a 0 or a 1. But that's not either what you want (I think ;).

Now, the solution might be this:

([01])\1*

which matches a 0 or a 1, and captures it. Then it matches any number of the same digit (\1 is a back reference to what ever is captured in the first capture group - in this case the 0 or the 1) any number of times.

Check it out at ideone.

Sign up to request clarification or add additional context in comments.

10 Comments

Your regex also, does not print anything (\[01])\1
Check the example added.
Added explanation to answer.
@Quota Correct, it captures the first digit. But if you check the OP's example, the full match is used - regexMatcher.group() (No number as parameter to group gives the complete match).
@Quota That may be true - it might be simpler to understand. And in that case you may even simplify it further by removing the capturing group - 0+|1+ which makes it (much) more efficient. The one I've given can easily be modified for other applications though (like matching ranges of letters (\w)\1*). But that's not really in the scope, so...
|
1

You can try this:

(1+|0+)

Explanation

Sample Code:

    final String regex = "(1+|0+)";
    final String string = "10000111000011\n"
            + "11001111110011";

    final Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
    final Matcher matcher = pattern.matcher(string);

    while (matcher.find()) {

                System.out.println("Group " + 1 + ": " + matcher.group(1));


    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.