In practice, you rarely need to create back-references to groups with numbers above 3 or 4, because when you need to juggle many groups you tend to create named capture groups. Normally, within a pattern, you create a back-reference to the content a capture group previously matched by using a backslash followed by the group number-for instance \1 for Group 1. How do Capture Groups Beyond \9 get Referenced? ![]() ✽ Relative Back-References and Forward-References ✽ Resetting Capture Groups like Variables (You Can't!) ✽ Generating New Capture Groups Automatically (You Can't!) ![]() ✽ Naming Groups-and referring back to them ✽ How do Capture Groups Beyond \9 get Referenced? But when it comes to numbering and naming, there are a few details you need to know, otherwise you will sooner or later run into situations where capture groups seem to behave oddly.įor easy navigation, here are some jumping points to various sections of the page: Yes, capture groups and back-references are easy and fun. You place a sub-expression in parentheses, you access the capture with \1 or $1… What could be easier?įor instance, the regex \b(\w+)\b\s+\1\b matches repeated words, such as regex regex, because the parentheses in (\w+) capture a word to Group 1 then the back-reference \1 tells the engine to match the characters that were captured by Group 1. Capture groups and back-references are some of the more fun features of regular expressions.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |