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

தொடக்கம் மற்றும் முடிவு: `^` மற்றும் `$`

Anchors are zero-width assertions: they match a position in the string, not a character. The two most basic ones are ^ (start of string) and $ (end of string).

Code
Pattern: ^Errore
Sample:  Errore: connessione persa
         ^^^^^^

Pattern: \.$
Sample:  Frase finale.
                     ^

^Errore matches the word Errore only if it is at the absolute start of the sample. \.$ matches a period only if it is the last character of the sample. Nothing more, nothing less.

Full validation

The most common idiom is to use both together: ^pattern$ matches only if the entire string corresponds to pattern. It is the basis of validation:

Code
^\d{4}$    a string of exactly 4 digits, nothing before, nothing after

Structured validation with start and end

Anchoring a pattern with ^ and $ is essential for validation. Without anchors, the regex \\d{5} will match inside abc1234567def, whereas with ^\\d{5}$ the string is rejected because the entire text must contain exactly 5 digits and nothing else.

Try it

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

Match the whole string ONLY if it contains exactly 4 digits, nothing before and nothing after.

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

Without anchors, \\d{4} would also accept '2024abc' or '12345' (first 4 digits).

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

Review exercise

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

Find the final period of the sentence, but ONLY if it is the last character of the sample.

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

\\.$ anchors the period to the end of the string. Without $ you would match the first period found.

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

Additional challenge

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

Create a pattern that matches only if the entire input string consists exclusively of one or more digits (no other characters at start or end).

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

Put ^ at the beginning and $ at the end of the \d+ pattern.

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