MCPcopy Create free account
hub / github.com/google/re2j / quoteMeta

Method quoteMeta

java/com/google/re2j/RE2.java:495–506  ·  view source on GitHub ↗

Returns a string that quotes all regular expression metacharacters inside the argument text; the returned string is a regular expression matching the literal text. For example, quoteMeta("[foo]").equals("\\[foo\\]").

(String s)

Source from the content-addressed store, hash-verified

493 * {@code quoteMeta("[foo]").equals("\\[foo\\]")}.
494 */
495 static String quoteMeta(String s) {
496 StringBuilder b = new StringBuilder(2 * s.length());
497 // A char loop is correct because all metacharacters fit in one UTF-16 code.
498 for (int i = 0, len = s.length(); i < len; i++) {
499 char c = s.charAt(i);
500 if ("\\.+*?()|[]{}^$".indexOf(c) >= 0) {
501 b.append('\\');
502 }
503 b.append(c);
504 }
505 return b.toString();
506 }
507
508 // The number of capture values in the program may correspond
509 // to fewer capturing expressions than are in the regexp.

Callers 3

testFowlerMethod · 0.95
testQuoteMetaMethod · 0.95
quoteMethod · 0.95

Calls 4

appendMethod · 0.80
lengthMethod · 0.45
indexOfMethod · 0.45
toStringMethod · 0.45

Tested by 2

testFowlerMethod · 0.76
testQuoteMetaMethod · 0.76