

Index methods provide useful index values that show precisely where the match was found in the input string: For convenience, the methods listed below are grouped according to functionality. Now that we have covered how character classes are created, You may want to review the Character Classes table before continuing with the next section.This section describes some additional useful methods of the Matcher class. I found the text "9" starting at index 0 and ending at index 1. I found the text "2" starting at index 0 and ending at index 1. This example creates a single character class that matches everything from 0 to 9, except the numbers 3, 4, and 5.

I found the text "5" starting at index 0 and ending at index 1.Īnd here is an example that shows the intersection of two ranges: Enter your regex: ]įinally, you can use subtraction to negate one or more nested character classes, such as ]. I found the text "4" starting at index 0 and ending at index 1. I found the text "3" starting at index 0 and ending at index 1. This particular intersection creates a single character class matching only the numbers common to both character classes: 3, 4, and 5. To create a single character class matching only the characters common to all of its nested classes, use &, as in ]. I found the text "8" starting at index 0 and ending at index 1. I found the text "6" starting at index 0 and ending at index 1. I found the text "0" starting at index 0 and ending at index 1. This particular union creates a single character class that matches the numbers 0, 1, 2, 3, 4, 6, 7, and 8. To create a union, simply nest one class inside the other, such as ]. You can also use unions to create a single character class comprised of two or more separate character classes. I found the text "foo6" starting at index 0 and ending at index 4. I found the text "foo5" starting at index 0 and ending at index 4. I found the text "foo1" starting at index 0 and ending at index 4. I found the text "c" starting at index 0 and ending at index 1. I found the text "b" starting at index 0 and ending at index 1. I found the text "a" starting at index 0 and ending at index 1. Here are some examples of ranges and negation: Enter your regex: For example, will match any letter of the alphabet: a to z (lowercase) or A to Z (uppercase). You can also place different ranges beside each other within the class to further expand the match possibilities. To specify a range, simply insert the "-" metacharacter between the first and last character to be matched, such as or. Sometimes you will want to define a character class that includes a range of values, such as the letters "a through h" or the numbers "1 through 5". The match is successful only if the first character of the input string does not contain any of the characters defined by the character class. I found the text "hat" starting at index 0 and ending at index 3. To match all characters except those listed, insert the "^" metacharacter at the beginning of the character class. In the above examples, the overall match succeeds only when the first letter matches one of the characters defined by the character class. I found the text "rat" starting at index 0 and ending at index 3. I found the text "cat" starting at index 0 and ending at index 3. I found the text "bat" starting at index 0 and ending at index 3. For example, the regular expression at will match the words "bat", "cat", or "rat" because it defines a character class (accepting either "b", "c", or "r") as its first character, followed by the two letters a and t. The most basic form of a character class is to simply place a set of characters side-by-side within square brackets. It specifies the characters that will successfully match a single character from a given input string.Īny character except a, b, or c (negation)Ī through z, or A through Z, inclusive (range)Ī through d, or m through p: (union)Ī through z, except for b and c: (subtraction)Ī through z, and not m through p: (subtraction)

In the context of regular expressions, a character class is a set of characters enclosed within square brackets. These classes have nothing to do with the Java classes in which you type your code. The word class is used in this table to denote character classes. The left-hand column specifies the regular expression constructs, while the right-hand column describes the conditions under which each construct will match. In this section you will find the following:

If you browse through the Pattern class specification, you will see tables summarizing the supported regular expression constructs.
