String Methods that accept Regular Expressions
Methods | Description |
+matches (regex: String): boolean |
Returns true if this string matches the pattern. |
+replaceAll (regex: String, replacement: String): String |
Returns a new string that replaces all matching substrings with the
replacement. |
+replaceFirst (regex: String, replacement: String): String |
Returns a new string that replaces the first matching substring with
the replacement. |
+split (regex: String): String[] |
Returns an array of strings consisting of the substrings split by the
matches. |
+split (regex: String, limit: int): String[] |
Same as the preceding split method except that the limit parameter
controls the number of times the pattern is applied. |
A regular expression is a way of denoting a pattern. In Java this
pattern is denoted by a String within the delimiters double quotes (").
Here are some definitions to start with:
Regular Expressions and Their Matches
Regular Expression | Matches |
. (period) | any character other than a line terminator |
(ab|cd) | ab or cd |
[abc] | a, b, or c |
[^abc] | any character except a, b, or c. |
[a-z] | any character from a to z (inclusive). |
\d | a digit same as [0-9] |
\D | not a digit, same as [^0-9] |
\w | a word character |
\W | not a word character |
\s | a white space character |
\S | a non white space character |
\b | word boundary |
\B | not a word boundary |
^p | begins with pattern p |
p$ | ends with pattern p |
p* | zero or more occurrences of pattern p |
p+ | one or more occurrences of pattern p |
p? | zero or one occurrence of pattern p |
p{n} | exactly n occurrences of pattern p |
p{n,} | at least n occurrences of pattern p |
p{n,m} | between n and m occurrences of
pattern p (inclusive) |