Search code examples
javaregexregex-lookarounds

how can i find alternating 1 and 0 using REGEX


the question is find all bit sequences which are alternating between 1 and 0, i.e., those that do not have more than 1 or 0 in a row and return it in a list.

what i did so far

  public static List<String> findBits(String text) {
    Pattern pattern = Pattern.compile("([01])(?!\\1)([01])(?:\\1\\2)*\\1?|(?<!\\S)[01](?!\\S)|1.|0.", Pattern.DOTALL);
    Matcher matcher = pattern.matcher(text);
    return matcher.results()
        .map(String::valueOf)
        .collect(Collectors.toList());

it should return

no binary numbers here 3434. -> [] Empty List

Hey friend this is a 1. -> [1]

Those are 1001, 1010, 1011, 1100, 1101 -> [1010]

This is a long value 1010101010 and this one as well 1010101010101011 -> [1010101010]

0 + 0 is a also a 0. -> [0,0,0]


Solution

  • You can use

    \b(?!\d*(\d)\1)[10]+\b
    

    See the regex demo.

    In Java, declare it with "\\b(?!\\d*(\\d)\\1)[10]+\\b".

    Details

    • \b - a word boundary
    • (?!\d*(\d)\1) - no repeated subsequent digits allowed in the current number
    • [10]+ - one or more 1 or 0 chars
    • \b - a word boundary

    See a Java demo:

    public static Pattern pattern = Pattern.compile("\\b(?!\\d*(\\d)\\1)[10]+\\b");
        
    public static List<String> findBits(String text) {
        Matcher matcher = pattern.matcher(text);
        return pattern.matcher(text)
            .results()
            .map(MatchResult::group)
            .collect(Collectors.toList()); //.toArray(String[]::new);
    }
            
    public static void main (String[] args) throws java.lang.Exception
    {
        List<String> r = findBits("no binary numbers here 3434. Hey friend this is a 1. Those are 1001, 1010, 1011, 1100, 1101. This is a long value 1010101010 and this one as well 1010101010101011. 0 + 0 is a also a 0.");
        System.out.println(r);
    }
    
    // => [1, 1010, 1010101010, 0, 0, 0]