2 hours ago · Tech · hide · 0 comments

Quick Recap # In Part 6, we learned about alternation with |, grouping with parentheses, and non-capturing groups (?:...). We saw that parentheses serve double duty: grouping for quantifiers and capturing matched text. Now let’s dig into the capturing side. Parentheses Capture What They Match # When you use regular parentheses (...), the engine saves whatever text matched inside them. The first set of parentheses is group 1, the second is group 2, and so on. The numbering goes left to right based on the opening parenthesis. Backreferences: Matching the Same Text Again # A backreference like \1 means “whatever group 1 actually matched.” Not the same pattern. The same text. This is a subtle but important distinction. ( # Group 1: capture a word [[:alpha:]]+ # One or more letters ) [[:blank:]] # A space \1 # Backreference: the SAME text group 1 matched Compact: ([[:alpha:]]+) \1 MATCH THESE the the is is hello hello DO NOT MATCH THESE the cat is was hello world This is how you find…

No comments yet. Log in to reply on the Fediverse. Comments will appear here.