Apply the regular expression to the content and return first match.

@param string $regex
@param string $content
@param int $i
@return array
337706
protected function _filter($regex, $content, $i = 1, $key = '') {
		if (@preg_match($regex, $content, $match) == 1) {
			$result = isset($match[$i]) ? $match[$i] : $match[0];
			if (isset($this->_callback['_all_'])) {
				foreach ($this->_callback['_all_'] as $filter) {
					$result = call_user_func($filter, $result);
				}
			}
			if ($key != '' && isset($this->_callback[$key])) {
				foreach ($this->_callback[$key] as $filter) {
					$result = call_user_func($filter, $result);
				}
			}
			return $result;
		}
		return false;
	}
1369780
public function getAllValues(array $array, string $regex)
    {
        $result = [];

        foreach ($array as $key => $value) {
            if (preg_match('/' . $regex . '/', $key)) {
                $result[ $key ] = $value;
            }
        }

        return $result;
    }
127316
protected function checkUriRegexMatch($regex, $returnOnTrue = null, $returnOnFalse = null)
    {
        if (\preg_match('#^' . $regex . '$#', $this->uri) === 1) {
            return $returnOnTrue ?? true;
        }

        return $returnOnFalse ?? false;
    }
924772
function(regex) {
  var self = this; var re = new RegExp(regex);
  var keys = {};
  self.init = function() { keys = {}; };
  self.accumulate = function(line) {
    var m = re.exec(line);
    if (m == null) return;
    var k = m[1]; // use 1st group as key
    var v = keys[k];
    if (v) keys[k] = v+1; else keys[k] = 1; // count by key
  };
  self.compensate = function(line) {
    var m = re.exec(line);
    if (m == null) return;
    var k = m[1]; // use 1st group as key
    var v = keys[k];
    if (v) keys[k] = v-1; else keys[k] = 1;
  };
  self.emit = function() {
    return keys;
  };
  self.make = function() { return new WideFinderFunction(regex); };
}
1273116
protected function matchUserAgent($regex)
    {
        // only match if useragent begins with given regex or there is no letter before it
        $regex = '/(?:^|[^A-Z0-9\-_]|[^A-Z0-9\-]_|sprd-)(?:' . str_replace('/', '\/', $regex) . ')/i';

        if (preg_match($regex, $this->userAgent, $matches)) {
            return $matches;
        }

        return false;
    }
1215814
public function links($regex = null)
    {
        /**
         * @var \DOMElement $node
         * @var \DOMElement $node2
         */
        $query = $this->_document->getQuery();
        $document = $this->_document;

        $data = [];
        foreach ($this->_nodes as $node) {
            if ($node->nodeName === 'a') {
                $href = $document->absolutizeUrl($node->getAttribute('href'));

                if ($regex && !preg_match($regex, $href)) {
                    continue;
                }

                $data[$node->getNodePath()] = ['href' => $href, 'text' => $node->textContent];
            } else {
                foreach ($query->xpath('descendant::a[@href]', $node) as $node2) {
                    $href = $document->absolutizeUrl($node2->getAttribute('href'));

                    if ($regex && !preg_match($regex, $href)) {
                        continue;
                    }

                    $data[$node2->getNodePath()] = ['href' => $href, 'text' => $node2->textContent];
                }
            }
        }

        return array_values($data);
    }
831012
public function capture(string $regex): MapInterface
    {
        if (\func_num_args() !== 1) {
            throw new LogicException('Offset and flags are no longer supported');
        }

        return RegExp::of($regex)->capture($this);
    }
1249733
public static String delFirst(String regex, CharSequence content) {
		if (StrUtil.hasBlank(regex, content)) {
			return StrUtil.str(content);
		}

		// Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
		final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
		return delFirst(pattern, content);
	}
936812
public static String replaceAll(CharSequence content, String regex, String replacementTemplate) {
		final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
		return replaceAll(content, pattern, replacementTemplate);
	}
1030952
private function getRanges(string $text, string $regex): array
	{
		preg_match_all($regex, $text, $matches, PREG_OFFSET_CAPTURE);

		$ranges = array_map(function ($match) {
			return [$match[1], strlen($match[0])];
		}, $matches[0]);

		return $ranges;
	}
426714
public function find($regexpPattern = '.*', $sort = false)
    {
        list(, $files) = $this->read($sort);

        return array_values(preg_grep('/^' . $regexpPattern . '$/i', $files));
    }
1156889
public static String extractRegexGroup(String fromContent, String regex) throws Exception {
        return extractRegexGroup(fromContent, regex, 1);
    }
785552
function matches( $pattern, $content, $id ) {
		$matches = null;
		if ( preg_match_all( $pattern, $content, $matches, PREG_OFFSET_CAPTURE ) > 0 ) {
			// Reduce memory usage by doing preg_replace() for the same $pattern/$replacement combination only once
			$content_replace = preg_replace( $pattern, $this->replace, $content );
			$results = [];
			foreach ( $matches[0] as $found ) {
				if ( empty( $found[0] ) ) {
					continue; // Some weird regex looking for nothing?
				}
				$result = new Result();
				$result->id = $id;
				$result->offset = $found[1];
				$result->length = strlen( $found[0] );
				// Extract the context - surrounding 40 characters either side
				// Index 0 is the match, index 1 is the position

				$start = $found[1] - 40;
				if ( $start < 0 ) {
					$start = 0;
				}

				$end = $found[1] + 40;
				if ( $end > strlen( $content ) ) {
					$end = strlen( $content );
				}

				$end -= $start;
				$left = ltrim( substr( $content, $start, $found[1] - $start ), " \t," );
				$right = rtrim( substr( $content, $found[1] + strlen( $found[0] ), $end ), " \t," );
				$result->left = $start;
				$result->left_length = strlen( $found[0] ) + ( $found[1] - $start ) + $end;
				if ( 0 !== $start ) {
					$result->search = '&hellip;';
				}
				$result->search .= esc_html( $left );
				$result->search .= '<del>' . esc_html( $found[0] ) . '</del>';
				$result->search .= esc_html( $right );
				$result->search_plain = esc_html( $left );
				$result->search_plain .= esc_html( $found[0] );
				$result->search_plain .= esc_html( $right );
				if ( $start + $end < strlen( $content ) ) {
					$result->search .= '&hellip;';
				}
				if ( ! is_null( $this->replace ) ) {
					// Produce preview
					$rep = preg_replace( $pattern, $this->replace, $found[0] );
					$result->replace_string = $rep;
					if ( 0 !== $start ) {
						$result->replace = '&hellip;';
					}
					$result->replace .= esc_html( $left );
					$result->replace .= '<ins>' . esc_html( $rep ) . '</ins></a>';
					$result->replace .= esc_html( $right );
					$result->left_length_replace = strlen( $left ) + strlen( $rep ) + strlen( $right ) + 1;
					$result->replace_plain  = esc_html( $left );
					$result->replace_plain .= esc_html( $rep );
					$result->replace_plain .= esc_html( $right );
					if ( $start + $end < strlen( $content ) ) {
						$result->replace .= '&hellip;';
					}
					// And the real thing
					$result->content = $content_replace;
				}
				$results[] = $result;
			}
			return $results;
		}
		return false;
	}
1306444
public function pageTextMatches($regex)
    {
        $actual = $this->session->getPage()->getText();
        $message = sprintf('The pattern %s was not found anywhere in the text of the current page.', $regex);

        $this->assertResponseText((bool) preg_match($regex, $actual), $message);
    }
614996
function matches(regex, str) {
    var ret = [];
    var match = regex.exec(str);
    while (match !== null) {
        ret.push({
            full: match[0],
            inner: match[1]
        });
        match = regex.exec(str);
    }
    return ret;
}
1279243
public static String get(String regex, CharSequence content, int groupIndex) {
		if (null == content || null == regex) {
			return null;
		}

		// Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
		final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL);
		return get(pattern, content, groupIndex);
	}
591992
protected function match(array $regexes, $string)
    {
        $pattern = '#(?<!like )('.join('|', $regexes).')#i';

        if (preg_match($pattern, $string, $matches)) {
            return $matches;
        }

        return false;
    }