Parse string by regexp or closure.
@param string $source
@param \Closure[]|string[]|\Closure|string $matchRules
@return false|float
493716
public static function parseString($source, $matchRules)
    {
        foreach ((array) $matchRules as $matchRule) {
            if (is_callable($matchRule) === true) {
                return call_user_func($matchRule, $source);
            } else {
                if (preg_match($matchRule, $source, $matches) === 1) {
                    $value = $matches['integral'];
                    if (isset($matches['fractional'])) {
                        $value .= '.' . $matches['fractional'];
                    }
                    return (double) $value;
                }
            }
        }
        return false;
    }
527430
protected function parseRuleString($ruleString)
    {
        if (is_string($ruleString)) {
            // Check for valid rule format
            // (with vendor prefix check to match e.g. "@-moz-document")
            if (preg_match(
                '/^[ \r\n\t\f]*@(' . self::getVendorPrefixRegExp("/") . ')?document[ \r\n\t\f]+(.*)$/i',
                $ruleString,
                $matches
            )) {
                $vendorPrefix = $matches[1];
                $ruleString = trim($matches[2], " \r\n\t\f");
                $charset = $this->getCharset();

                $inFunction = false;
                $isEscaped = false;

                $conditions = [];
                $currentCondition = "";
                $currentValue  = "";
                for ($i = 0, $j = mb_strlen($ruleString, $charset); $i < $j; $i++) {
                    $char = mb_substr($ruleString, $i, 1, $charset);
                    if ($char === "\\") {
                        if ($isEscaped === false) {
                            $isEscaped = true;
                        } else {
                            $isEscaped = false;
                        }
                    } else {
                        if ($char === "(") {
                            if ($isEscaped === false) {
                                $inFunction = true;
                                continue;
                            } else {
                                $currentValue .= $char;
                            }
                        } else if ($char === ")") {
                            if ($isEscaped === false) {
                                $conditions[$currentCondition] = trim($currentValue, " \r\n\t\f");
                                $currentCondition = "";
                                $currentValue = "";
                                $inFunction = false;
                                continue;
                            } else {
                                $currentValue .= $char;
                            }
                        } else if ($char === "," || $char === " ") {
                            if ($currentCondition === "" && $currentValue === "") {
                                continue;
                            } elseif ($currentValue !== "") {
                                $currentValue .= $char;
                            } else {
                                // something wrong here...
                            }
                        } else {
                            if ($inFunction === false) {
                                $currentCondition .= $char;
                            } else {
                                $currentValue .= $char;
                            }
                        }
                    }

                    // Reset escaped flag
                    if ($isEscaped === true && $char !== "\\") {
                        $isEscaped = false;
                    }
                }

                foreach ($conditions as $key => $value) {
                    $conditions[$key] = Placeholder::replaceStringPlaceholders($value, true);
                }

                if (isset($conditions["url"])) {
                    $this->setUrl($conditions["url"]);
                }
                if (isset($conditions["url-prefix"])) {
                    $this->setUrlPrefix($conditions["url-prefix"]);
                }
                if (isset($conditions["domain"])) {
                    $this->setDomain($conditions["domain"]);
                }
                if (isset($conditions["regexp"])) {
                    $this->setRegexp($conditions["regexp"]);
                }
                if ($vendorPrefix !== "") {
                    $this->setVendorPrefix($vendorPrefix);
                }
            } else {
                throw new \InvalidArgumentException("Invalid format for @document rule.");
            }
        } else {
            throw new \InvalidArgumentException(
                "Invalid type '" . gettype($ruleString) . "' for argument 'ruleString' given. String expected."
            );
        }
    }
364788
protected function determineMatches(Matcher $matcher, Result $control, array $trials = [])
    {
        foreach ($trials as $trial) {
            if ($matcher->match($control->getValue(), $trial->getValue())) {
                $trial->setMatch(true);
            }
        }
    }
1318224
public function parse($source)
    {
        $result = false;
        foreach ($this->parsers as $parser) {
            try {
                $result = $parser->parse($source);
                if ($result instanceof \DOMDocument) {
                    break;
                }
            } catch (\Exception $e) {
                $this->logException($e);
            }
        }
        if (!($result instanceof \DOMDocument)) {
            throw new \DOMException('Unable to parse source');
        }
        return $result;
    }
1323761
private function checkByline($node, $matchString)
    {
        if (!$this->configuration->getArticleByLine()) {
            return false;
        }

        /*
         * Check if the byline is already set
         */
        if ($this->getAuthor()) {
            return false;
        }

        $rel = $node->getAttribute('rel');

        if ($rel === 'author' || preg_match(NodeUtility::$regexps['byline'], $matchString) && $this->isValidByline($node->getTextContent())) {
            $this->logger->info(sprintf('[Metadata] Found article author: \'%s\'', $node->getTextContent()));
            $this->setAuthor(trim($node->getTextContent()));

            return true;
        }

        return false;
    }
1159647
public function getMatchAll(string $pattern, int $flags = PREG_PATTERN_ORDER, int $offset = 0)
	{
		return preg_match_all($pattern, $this->str, $matches, $flags, $offset) === false ? null : $matches;
	}
65495
public function filter($source)
    {
        // clean source
        foreach ((array) $source as $sourceName => $sourceValue) {
            $source[ltrim($sourceName, ':')] = $sourceValue;
        }

        $pregQuotedTargetReplacementIdentifier = preg_quote($this->targetReplacementIdentifier, '#');
        $processedParts = [];

        foreach ($this->rules as $ruleName => $ruleValue) {
            if (isset($source[$ruleName])) {
                if (is_string($ruleValue)) {
                    // overriding the set rule
                    $processedParts['#' . $pregQuotedTargetReplacementIdentifier . $ruleName . '#'] = str_replace('\\', '\\\\', $source[$ruleName]);
                } elseif (is_array($ruleValue)) {
                    $processedPart = $source[$ruleName];
                    foreach ($ruleValue as $ruleFilter) {
                        $processedPart = $ruleFilter($processedPart);
                    }
                    $processedParts['#' . $pregQuotedTargetReplacementIdentifier . $ruleName . '#'] = str_replace('\\', '\\\\', $processedPart);
                }
            } elseif (is_string($ruleValue)) {
                $processedParts['#' . $pregQuotedTargetReplacementIdentifier . $ruleName . '#'] = str_replace('\\', '\\\\', $ruleValue);
            }
        }

        // all of the values of processedParts would have been str_replace('\\', '\\\\', ..)'d to disable preg_replace backreferences
        $inflectedTarget = preg_replace(array_keys($processedParts), array_values($processedParts), $this->target);

        if ($this->throwTargetExceptionsOn && (preg_match('#(?=' . $pregQuotedTargetReplacementIdentifier.'[A-Za-z]{1})#', $inflectedTarget) == true)) {
            throw new Exception\RuntimeException('A replacement identifier ' . $this->targetReplacementIdentifier . ' was found inside the inflected target, perhaps a rule was not satisfied with a target source?  Unsatisfied inflected target: ' . $inflectedTarget);
        }

        return $inflectedTarget;
    }
261636
protected function pregMatchArray($input, $rules, $flags = 0)
    {
        if (!is_array($rules)) {
            return false;
        }

        foreach ($rules as $rule => $value) {
            if (preg_match($rule, $input, $matches, $flags)) {
                return array_merge($matches, ['value' => $value]);
            }
        }

        return false;
    }
610782
public function matches(string $regex): bool
    {
        if (\func_num_args() !== 1) {
            throw new LogicException('Offset is no longer supported');
        }

        return RegExp::of($regex)->matches($this);
    }
1328116
public function match($source)
    {
        $this->factory->setType(Token::END);

        return parent::match($source);
    }
16749
public function format($mask, callable $matchHandler)
    {
        try {
            // Run a regular expression to match the mask
            return preg_replace_callback('/%(.*?)%/i', function ($matches) use ($matchHandler) {
                // Return the result returned from the matchHandler
                return $this->getResult($matches[1], $matchHandler) ?: '';
            }, $mask);
        } catch (\Exception $e) {
            // Log any failures
            $this->logger->debug(
                'Failed to format express mask "{mask}": {message}',
                ['mask' => $mask, 'message' => $e->getMessage(), 'exception' => $e]
            );
        }
    }
1003273
public static function parse($source) {
        $name = null;
        $value = null;

        if (preg_match_all('@(<\S*?'.'>)@', $source, $matches)) {
            $name = $matches[0][0];
        }
        if (preg_match_all('@\[default: (.*)\]@i', $source, $matches)) {
            $value = $matches[0][1];
        }
        return new static($name, $value);
    }
443896
public function match($source, TokenFactoryInterface $factory)
    {
        $all = [];

        foreach ($this->multiLine as $name => $comment) {
            $comment = array_map(function ($e) { return preg_quote($e, '/'); }, $comment);

            $all[] = [$name, "/({$comment[0]}(.*?){$comment[1]})/ms"];
        }

        foreach ($this->singleLine as $name => $comment) {
            $comment = preg_quote($comment, '/');
            $all[]   = [$name, "/({$comment}[^\r\n]*)/"];
        }

        foreach ($all as $i => $comment) {
            $matches = [];

            $name  = $comment[0];
            $name  = is_string($name) ? $name : null;
            $regex = $comment[1];

            if (preg_match_all($regex, $source, $matches, PREG_OFFSET_CAPTURE)) {
                foreach ($matches[1] as $match) {
                    yield $factory->create(
                        $name, ['pos' => $match[1], 'length' => strlen($match[0])]
                    );
                }
            }
        }
    }
438431
function regexp($pattern, $flags = null) {
        $regexp['pattern'] = $pattern;
        if ($flags) $regexp['flags'] = $flags;
        $this->data['regexp'] = $regexp;
        return $this;
    }
919606
public function setIndividuationMenu($menus, $matchrule)
    {
        if ($menus instanceof Closure) {
            $menus = $menus($this);
        }

        if (!is_array($menus)) {
            $this->setError('子菜单必须是数组或者匿名函数返回数组');

            return false;
        }
        $menus = $this->extractMenus($menus);

        $data = [
            'button'    => $menus,
            'matchrule' => $matchrule,
        ];

        $res = $this->_post('addconditional', $data);

        return $res;
    }
914980
public function matchPath($pattern, &$matches = null, $flags = 0, $offset = 0)
	{
		return preg_match($pattern, $this->_path ?: '', $matches, $flags, $offset);
	}
850574
public function matching ($pattern, &$matches = [], $flags = 0, $offset = 0, $start = 0, $end = null) {
		if (is_null($end)) {
			$end = $this->_length;
		}
		return static::Match($pattern, static::Substring($this->_string, $start, $end), $matches, $flags, $offset);
	}