முக்கிய உள்ளடக்கத்திற்குச் செல்லவும்
eLearner.app
தொகுதி 4 · பாடம் 1 இன் 4பாடத்திட்டத்தில் 13/32~10 min
தொகுதி பாடங்கள் (1/4)

குழுக்களைப் பிடிக்கவும்: `(...)`

Parentheses (...) have two functions in one:

  1. Group sub-patterns, so that a quantifier applies to the whole group ((ab)+ matches ab, abab, ababab).
  2. Capture the matched substring, exposing it as a numbered group accessible separately (group 1, 2, 3… from left to right based on the opening parenthesis).
Code
Pattern: (\d{4})-(\d{2})-(\d{2})
Sample:  Data: 2024-03-15.
               ^^^^^^^^^^
Gruppi:
  match  = "2024-03-15"
  gruppo1 = "2024"
  gruppo2 = "03"
  gruppo3 = "15"

The editor on the right shows the groups under each match.

Quantifying a group

Without parentheses, ab+ means "a followed by one or more b". With parentheses it becomes "one or more ab":

Code
Pattern   Matcha su 'ababab':
ab+       'ab' (poi 'a' e 'b' separati, non collegati)
(ab)+     'ababab' come gruppo unico

Capture group indices and usage

Each pair of parentheses not preceded by special characters creates a numbered capture group. Group 1 corresponds to the first open parenthesis, group 2 to the second, and so forth. In JavaScript, you can access these groups via the array returned by exec or matchAll.

Try it

உடற்பயிற்சி#regex.m4.l1.e1
முயற்சிகள்: 0ஏற்றுகிறது…

Find every date in YYYY-MM-DD format, capturing year, month and day as three separate groups.

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

Wrap each of the three digit blocks in parentheses: each one creates a numbered group.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்

Review exercise

உடற்பயிற்சி#regex.m4.l1.e2
முயற்சிகள்: 0ஏற்றுகிறது…

Find every repetition of `ab` (one or more) as a single match. Without parentheses, the quantifier would apply only to the last `b`.

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

(ab)+ treats 'ab' as a unit and looks for one or more consecutive repetitions.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்

Additional challenge

உடற்பயிற்சி#regex.m4.l1.e3
முயற்சிகள்: 0ஏற்றுகிறது…

Create three capture groups to extract the day, month, and year separately from dates in the format `DD/MM/YYYY`.

எடிட்டரை ஏற்றுகிறது…
குறிப்பைக் காட்டு

Enclose each \d{2} or \d{4} in parentheses.

3 முயற்சிகளுக்குப் பிறகு தீர்வு கிடைக்கும்