The following example uses the ^ boundary matcher metacharacter to ensure that a line begins with The followed by zero or more word characters: java RegexDemo "^The\w*" Therefore \Z: The end of the text, except for the final line terminator (if any). You can accomplish this task by using one of Pattern's boundary matchers, which are regex constructs that identify match locations: We sometimes want to match patterns at the beginning of lines, at word boundaries, at the end of text, and so on. The output below shows what RegexDemo's matcher finds: regex = (Java( language)\2)įound starting at 4 and ending at 25 Boundary matchers The \2 back reference recalls number 2's saved match, which allows the matcher to search for a second occurrence of a space character followed by language, which immediately follows the first occurrence of the space character and language. The regex specifies two capturing groups: number 1 is (Java( language)\2), which matches Java language language, and number 2 is ( language), which matches a space character followed by language. The example uses the (Java( language)\2) regex to search the input text " The Java language language" for a grammatical error, where Java immediately precedes two consecutive occurrences of language. The following example demonstrates the usefulness of a back reference in searching text for a grammatical error: java RegexDemo "(Java( language)\2)" "The Java language language" The presence of a back reference causes a matcher to use the back reference's capturing group number to recall the capturing group's saved match, and then use that match's characters to attempt a further match operation. Specified as a backslash character followed by a digit character denoting a capturing group number, the back reference recalls a capturing group's captured text characters. In (a)(b), (a) belongs to capturing group number 1, and (b) belongs to capturing group number 2.Įach capturing group saves its match for later recall by a back reference. In the example, (Java( language)) belongs to capturing group number 1, and ( language) belongs to capturing group number 2. Each nested or non-nested capturing group receives its own number, numbering starts at 1, and capturing groups are numbered from left to right. For example, in the (Java( language)) regex, ( language) nests inside (Java). Each match replaces the previous match's saved Java characters with the next match's Java characters.Ĭapturing groups can be nested inside other capturing groups. This capturing group matches the Java pattern against all occurrences of Java in the input text. For example, the (Java) capturing group combines letters J, a, v, and a into a single unit. All characters within the capturing group are treated as a single unit during pattern matching.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |