If the given regexes matches exactly one string, returns that string. Otherwise returns {@code
null}. This can be used to identify arguments to e.g. {@code String.replaceAll} that don't need
to be regexes.
328976
public static Optional<String> convertRegexToLiteral(String s) {
    try {
      Pattern.compile(s);
    } catch (PatternSyntaxException e) {
      /* The string is a malformed regular expression which will throw an error at runtime. We will
       * preserve this behavior by not rewriting it.
       */
      return Optional.empty();
    }

    boolean inQuote = false;
    StringBuilder result = new StringBuilder();
    int length = s.length();
    for (int i = 0; i < length; ++i) {
      char current = s.charAt(i);
      if (!inQuote && UNESCAPED_CONSTRUCT.matches(current)) {
        /* If we see an unescaped regular expression control character then we can't match this as a
         * string-literal so give up
         */
        return Optional.empty();
      } else if (current == '\\') {

        /* There should be a character following the backslash. No need to check for string length
         * since we have already ascertained we have a well formed regex */
        char escaped = s.charAt(++i);

        if (escaped == 'Q') {
          inQuote = true;
        } else if (escaped == 'E') {
          inQuote = false;
        } else {
          /* If not starting or ending a quotation (\Q...\E) backslashes can only be used to write
           * escaped constructs or to quote characters that would otherwise be interpreted as
           * unescaped constructs.
           *
           * If they are escaping a construct we can write as a literal string (i.e. one of \t \n
           * \f \r or \\) then we convert to a literal character.
           *
           * If they are escaping an unescaped construct we convert to the relevant character
           *
           * Everything else we can't represent in a literal string
           */
          Character controlChar = REGEXCHAR_TO_LITERALCHAR.get(escaped);
          if (controlChar != null) {
            result.append(controlChar);
          } else if (escaped == '\\') {
            result.append('\\');
          } else if (UNESCAPED_CONSTRUCT.matches(escaped)) {
            result.append(escaped);
          } else {
            return Optional.empty();
          }
        }
      } else {
        /* Otherwise we have a literal character to match so keep going */
        result.append(current);
      }
    }
    return Optional.of(result.toString());
  }
612492
boolean matches(String value) {
    if (pattern == null) {
      return true;
    }
    return pattern.matcher(value).matches();
  }
746793
@Requires("isValidPattern(pattern)")
  public R get(String pattern) {
    String canon = pattern.replace('/', '.');
    boolean exact = !canon.endsWith(".*");
    if (!exact) {
      canon = canon.substring(0, canon.length() - 2);
    }

    String[] parts = canon.split("\\.");
    TernaryNode current = root;
    R best = null;
    for (int i = 0; i < parts.length; ++i) {
      TernaryNode next = current.children.get(parts[i]);
      if (next == null) {
        break;
      }
      if (next.rule != null) {
        if (next.exact) {
          if (exact && i == parts.length - 1) {
            best = next.rule;
          }
        } else {
          best = next.rule;
        }
      }
      current = next;
    }
    return best;
  }
911165
public static boolean regexMatch(String input, String regex) {
        return Pattern.compile(regex).matcher(input).matches();
    }
890795
public static boolean matchesAny(String str, List<String> regexes) {
        return matchesAny(str, regexes, true);
    }
157152
protected Match<?> match() {
        Match<?> m = result().getMatch();
        if (m == null) {
            throw new IllegalStateException("Match failed");
        }
        return m;
    }
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);
	}
286159
public char match(String str1, String str2) {
        if (str1 == null || str2 == null)
            return IMappingElement.IDK;
        if (str1.equals(str2)) {
            return IMappingElement.EQUIVALENCE;
        } else
            return IMappingElement.IDK;
    }
560427
public CollectionAssert allElementsMatch(String regex) {
		isNotNull();
		isNotEmpty();
		for (Object anActual : this.actual) {
			if (anActual == null) {
				failWithMessageRelatedToRegex(regex, anActual);
			}
			String value = anActual.toString();
			if (!value.matches(regex)) {
				failWithMessageRelatedToRegex(regex, value);
			}
		}
		return this;
	}
959889
public static boolean isValidStringMatchingRegex(final String value, final String expression) {

		Pattern pattern = patterns.get(expression);

		if (pattern == null) {

			pattern = Pattern.compile(expression);
			patterns.put(expression, pattern);
		}

		return (value != null && pattern.matcher(value).matches());
	}
668583
public T match(CharSequence text, boolean matchPrefix)
    {
        if (root == null)
        {
            throw new IllegalStateException("not compiled");
        }
        int length = text.length();
        for (int ii=0;ii<length;ii++)
        {
            switch (match(text.charAt(ii)))
            {
                case Error:
                    return null;
                case Ok:
                    if (matchPrefix)
                    {
                        T uniqueMatch = state.getUniqueMatch();
                        if (uniqueMatch != null)
                        {
                            state = root;
                            return uniqueMatch;
                        }
                    }
                    break;
                case Match:
                    return getMatched();
            }
        }
        return null;
    }
682937
public static ExpectedCondition<Boolean> urlMatches(final String regex) {
    return new ExpectedCondition<Boolean>() {
      private String currentUrl;
      private Pattern pattern;
      private Matcher matcher;

      @Override
      public Boolean apply(WebDriver driver) {
        currentUrl = driver.getCurrentUrl();
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(currentUrl);
        return matcher.find();
      }

      @Override
      public String toString() {
        return String
          .format("url to match the regex \"%s\". Current url: \"%s\"", regex, currentUrl);
      }
    };
  }
791179
@Deprecated
  public static PactDslRootValue stringMatcher(String regex) {
    PactDslRootValue rootValue = new PactDslRootValue();
    rootValue.generators.addGenerator(Category.BODY, "", new RegexGenerator(regex));
    rootValue.setValue(new Generex(regex).random());
    rootValue.setMatcher(rootValue.regexp(regex));
    return rootValue;
  }
627686
function(regex){
		//if it isn't global, `exec()` will not start at `lastIndex`
		if(!regex.global)
			regex.compile(regex.source, flags(regex));
		regex.lastIndex = this.position;
		var m = regex.exec(this.source);

		//all matches must start at the current position.
		if(m && m.index!=this.position){
			return null;
		}

		if(m) this.position += m[0].length;
		return m;
	}
717221
public Stream<HString> findAllPatterns(@NonNull String regex) {
      return findAllPatterns(Pattern.compile(regex));
   }
1153046
function (regexp) {
        var res = maybeCallNative(nativeSearch, regexp, this);
        if (res.done) return res.value;

        var rx = anObject(regexp);
        var S = String(this);

        var previousLastIndex = rx.lastIndex;
        if (!sameValue(previousLastIndex, 0)) rx.lastIndex = 0;
        var result = regExpExec(rx, S);
        if (!sameValue(rx.lastIndex, previousLastIndex)) rx.lastIndex = previousLastIndex;
        return result === null ? -1 : result.index;
      }
706295
protected boolean check(String value, String regex) {
		Pattern pattern = Pattern.compile(regex);
		return pattern.matcher(value).matches();
	}