explode an array by lines that match a regular expression

@param string[] $list  a flat array
@param string $pattern a regular expression

@return string[]
489008
public static function pregSplitArray(array $list, string $pattern): array
    {
        $slices = [];
        $index = -1;
        foreach ($list as $value) {
            if (preg_match($pattern, $value) === 1) {
                ++$index;
            }

            if ($index !== -1) {
                $slices[$index][] = $value;
            }
        }

        return $slices;
    }
950606
public static function getValidKey($pattern, $arr) {
        $keys = array();
        foreach($arr as $key => $value) {
            if (preg_match($pattern, $key)){
                $keys[] = $key;
            }
        }

        return $keys;
    }
1270556
protected function match(array &$str, $pattern, $keep)
    {
        $tokens = array();
        foreach ($str as $s) {
            preg_match_all($pattern, $s, $m);
            $tokens = array_merge(
                $tokens,
                $m[$keep]
            );
        }

        $str = $tokens;
    }
1152343
protected function buildBuilders($pattern, array $matches)
    {
        $result = [
            'pattern' => $pattern,
            'segments' => []
        ];

        foreach ($matches as $match) {
            list($key,) = $this->splitSegment($match[3]);
            $result['pattern'] = str_replace($match[0], '{' . $key . '}', $result['pattern']);
            $result['segments']['{' . $key . '}'] = '{value}' . $match[4] . (isset($match[7]) ? $match[7] : '');
        }

        return $result;
    }
131982
public function split(string $pattern, int $limit = -1, int $flags = 0): TextVector
    {
        $pieces = preg_split($pattern, $this->text, $limit, $flags);

        $pieces = (array) $pieces;

        return TextVector::make($pieces);
    }
1212686
private function match_regex($value, $pattern, $rules = array())
    {
        $pieces = explode(':', $pattern);
        if(count($pieces) <= 1)
            return false;

        $regex = $pieces[1];

        if (array_key_exists($regex, $rules)) {
            $regex = $rules[$regex];
        }

        // ritorna vero se il valore
        // rispetta l'espressione regolare
        return preg_match( "/$regex/", $value );
    }
209076
def match_regex_list(patterns, string):
    """"""

    for p in patterns:

        if re.findall(p, string):
            return True

    return False
413598
public static function getByPattern(array $bundles, $pattern)
    {
        $pattern = trim($pattern, '/');
        $items = [];
        foreach ($bundles as $bundle) {
            /**
             * @var $bundle RouteBundle
             */
            if ($bundle->getPattern() == $pattern) {
                $items = $bundle;
            }
        }
        return $items;
    }
1273611
public function matches(array $list)
    {
        $list = array_values($list);  // Ensure that we have an indexed array
        if ($this->isSizeValid($list)) {
            $mismatch = false;
            foreach ($this->chain as $i => $search) {
                $proxy_url = $list[$i];
                if (preg_match('/^\/.*\/[ixASUXu]*$/s', $search)) {
                    if (preg_match($search, $proxy_url)) {
                        phpCAS::trace(
                            "Found regexp " .  $search . " matching " . $proxy_url
                        );
                    } else {
                        phpCAS::trace(
                            "No regexp match " .  $search . " != " . $proxy_url
                        );
                        $mismatch = true;
                        break;
                    }
                } else {
                    if (strncasecmp($search, $proxy_url, strlen($search)) == 0) {
                        phpCAS::trace(
                            "Found string " .  $search . " matching " . $proxy_url
                        );
                    } else {
                        phpCAS::trace(
                            "No match " .  $search . " != " . $proxy_url
                        );
                        $mismatch = true;
                        break;
                    }
                }
            }
            if (!$mismatch) {
                phpCAS::trace("Proxy chain matches");
                return true;
            }
        } else {
            phpCAS::trace("Proxy chain skipped: size mismatch");
        }
        return false;
    }
1255882
protected function parsePattern($pattern)
    {
        $currentPos      = 0;
        $length          = strlen($pattern);
        $tokens          = [];
        $level           = 0;
        $quotedDelimiter = preg_quote($this->delimiter);

        while ($currentPos < $length) {
            preg_match('(\G(?P<literal>[^:{\[\]]*)(?P<token>[:\[\]]|$))', $pattern, $matches, 0, $currentPos);

            $currentPos += strlen($matches[0]);

            if (!empty($matches['literal'])) {
                $tokens[] = ['literal', $matches['literal']];
            }

            if ($matches['token'] === ':') {
                if (!preg_match(
                    '(\G(?P<name>[^:' . $quotedDelimiter . '{\[\]]+)(?:{(?P<delimiters>[^}]+)})?:?)',
                    $pattern,
                    $matches,
                    0,
                    $currentPos
                )) {
                    throw new Exception\RuntimeException('Found empty parameter name');
                }

                $tokens[] = [
                    'parameter',
                    $matches['name'],
                    isset($matches['delimiters']) ? $matches['delimiters'] : null
                ];

                $currentPos += strlen($matches[0]);
            } elseif ($matches['token'] === '[') {
                $tokens[] = ['optional-start'];
                $level++;
            } elseif ($matches['token'] === ']') {
                $tokens[] = ['optional-end'];
                $level--;

                if ($level < 0) {
                    throw new Exception\RuntimeException('Found closing bracket without matching opening bracket');
                }
            } else {
                break;
            }
        }

        if ($level > 0) {
            throw new Exception\RuntimeException('Found unbalanced brackets');
        }

        return $tokens;
    }
59736
private function findIn(array $collection, $index, $pattern)
    {
        $total = count($collection);
        while ($index < $total) {
            $found = $this->match($pattern, $collection, $index++);
            if ($found !== false) {
                $token = $collection[$found];

                return $token->getLineNumber();
            }
        }

        return false;
    }
579228
protected function convert(/*# string */ $pattern)/*# : array */
    {
        $ph = sprintf("\{%s(?:%s)?\}", self::MATCH_GROUP_NAME, self::MATCH_GROUP_TYPE);

        // count placeholders
        $map = $m = [];
        if (preg_match_all('~'. $ph .'~x', $pattern, $m)) {
            $map = $m[1];
        }

        $result = preg_replace(
            [
            '~' . $ph . '(*SKIP)(*FAIL) | \[~x', '~' . $ph . '(*SKIP)(*FAIL) | \]~x',
            '~\{' . self::MATCH_GROUP_NAME . '\}~x', '~' . $ph . '~x',
            ],
            ['(?:', ')?', '{\\1:' . self::MATCH_SEGMENT . '}', '(\\2)'],
            strtr('/' . trim($pattern, '/'), $this->shortcuts)
        );

        return [$result, $map];
    }
364263
public static function filterArrayValuesWithPattern(array $values, $pattern)
    {
        return array_values(
            array_filter(
                array_map(function ($key) use ($pattern) {
                    if ((bool) preg_match('/'.$pattern.'/', $key)) {
                        return $key;
                    }
                }, $values)
            )
        );
    }
242650
public function select(string $pattern = '', int $count = 100): \Iterator
    {
        $iterator = null;
        while (true) {
            $ret = $this->handle->hScan($this->name, $iterator, $pattern, $count);
            if (false === $ret) {
                break;
            }
            foreach ($ret as $k => $v) {
                yield [$k => $v];
            }
        }
    }
145051
public function hash(array $list)
    {
        $ids = '';
        foreach ($list as $item) {
            $ids .= ':'.$item['id'];
        }
        return md5($ids);
    }
193512
protected function parsePlaceholders($pattern)
    {
        $params = [];
        $parser = $this->parser;

        preg_match_all("~" . $parser::DYNAMIC_REGEX . "~x", $pattern, $matches, PREG_SET_ORDER);

        foreach ((array) $matches as $key => $match) {
            $pattern = str_replace($match[0], isset($match[2]) ? "({$match[2]})" : "([^/]+)", $pattern);
            $params[$key] = $match[1];
        }

        return [$pattern, $params];
    }
856090
public function parse(string $list)
    {
        if (!$list) {
            return $list;
        }

        $list = collect(explode(',', $list));

        return $list->map(function ($item) {
            $parts = explode(':', $item);
            $model = app()->make(config('mentions.pools.'.$parts[0].'.model'));
            return $model::findOrFail($parts[1]);
        });
    }