4

I have binary pattern "10000101001" and I want to match string having 0 in starting and ending 1. So for above given example there should be 3 possible match 100001, 101, 1001.

Below is the sample code I am trying:

function solution() {
    $length = 0;
    $decStr = "10000101001";
    $pattern = "/[1][^1]0*[1]/";
    preg_match_all($pattern, $decStr, $matches);
    echo "<pre>";
    print_r($matches);
    echo "</pre>";
}

This gives output as

Array
(
    [0] => Array
        (
            [0] => 100001
            [1] => 1001
        )

)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Manoj Saini
  • 108
  • 4

1 Answers1

4

Positive lookahead

/(?=(1[^1]+1))/

Read this: http://www.regular-expressions.info/lookaround.html

Deep
  • 2,472
  • 2
  • 15
  • 25