![]() I found the text "aaaa" starting at index 4 and ending at index 8.Įven though the letter "b" appears in cells 1, 3, and 8, the output reports a zero-length match at those locations. I found the text "" starting at index 9 and ending at index 9. I found the text "" starting at index 8 and ending at index 8. I found the text "a" starting at index 7 and ending at index 8. I found the text "a" starting at index 6 and ending at index 7. I found the text "a" starting at index 5 and ending at index 6. I found the text "" starting at index 3 and ending at index 3. ![]() For example, what happens if it encounters the letter "b", as in "ababaaaab"? And finally, a+ matches all occurrences of the letter "a", ignoring the presence of "nothing" at the last index.Īt this point, you might be wondering what the results would be if the first two quantifiers encounter a letter other than "a". The expression a* finds two separate matches: all of the letter "a"'s in the first match, then the zero-length match after the last character at index 5. The expression a? finds an individual match for each character, since it matches when "a" appears zero or one times. I found the text "aaaaa" starting at index 0 and ending at index 5. I found the text "" starting at index 5 and ending at index 5. I found the text "a" starting at index 4 and ending at index 5. I found the text "a" starting at index 3 and ending at index 4. I found the text "a" starting at index 2 and ending at index 3. I found the text "a" starting at index 1 and ending at index 2. Now change the input string to the letter "a" five times in a row and you'll get the following: Depending on the quantifier used, the presence of "nothing" at the index after the last character may or may not trigger a match. Remember, the matcher sees the character "a" as sitting in the cell between index 0 and index 1, and our test harness loops until it can no longer find a match. I found the text "" starting at index 1 and ending at index 1.Īll three quantifiers found the letter "a", but the first two also found a zero-length match at index 1 that is, after the last character of the input string. I found the text "a" starting at index 0 and ending at index 1. Change the input string to a single letter "a" and you'll notice something interesting: Let's explore zero-length matches with a few more examples. Zero-length matches are easily identifiable because they always start and end at the same index position. A zero-length match can occur in several cases: in an empty input string, at the beginning of an input string, after the last character of an input string, or in between any two characters of an input string. Matches of this sort are known as a zero-length matches. The empty input string "" has no length, so the test simply matches nothing at index 0. You'll also notice that the start and end indices are both zero, which is unlike any of the examples we've seen so far. In the above example, the match is successful in the first two cases because the expressions a? and a* both allow for zero occurrences of the letter a. This particular union creates a single character class that matches the numbers 0, 1, 2, 3, 4, 6, 7, and 8.I found the text "" starting at index 0 and ending at index 0. 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. Here are some examples of ranges and negation: 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'll 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |