0% found this document useful (0 votes)
4 views

Regular Expression Syntax

Uploaded by

tom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Regular Expression Syntax

Uploaded by

tom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Anchors
Anchors specify positions in the input text.

Pattern Description Example

^ Matches the start of a string. ^Hello matches "Hello World".

$ Matches the end of a string. World$ matches "Hello World".

\b Matches a word boundary. \bcat\b matches "cat" but not


"catalog".

\B Matches a non-word \Bcat matches "catalog" but not "cat".


boundary.

2. Character Classes
Character classes match specific sets of characters.

Pattern Description Example

[abc] Matches any one of the characters a, b, or [aeiou] matches any vowel.
c.

[^abc] Matches any character except a, b, or c. [^aeiou] matches any


consonant.

[a-z] Matches any character from a to z. [0-9] matches any digit.

. Matches any character except a newline. a.c matches "abc", "a#c".

3. Predefined Character Classes


Python provides shorthand character classes for common sets.

Pattern Description Example

\d Matches any digit (equivalent to [0-9]). \d+ matches "123".


\D Matches any non-digit. \D+ matches "abc".

\w Matches any word character (letters, digits, \w+ matches


_). "hello_world".

\W Matches any non-word character. \W+ matches "@#$".

\s Matches any whitespace character. \s+ matches spaces, tabs.

\S Matches any non-whitespace character. \S+ matches "hello".

4. Quantifiers
Quantifiers specify the number of occurrences for a match.

Pattern Description Example

* Matches 0 or more repetitions. ab*c matches "ac", "abc", "abbc".

+ Matches 1 or more repetitions. ab+c matches "abc", "abbc".

? Matches 0 or 1 repetition. ab?c matches "ac", "abc".

{n} Matches exactly n occurrences. a{3} matches "aaa".

{n,} Matches n or more occurrences. a{2,} matches "aa", "aaa", etc.

{n,m} Matches between n and m a{2,4} matches "aa", "aaa",


occurrences. "aaaa".

5. Groups and Backreferences


Groups allow you to capture parts of the match.

Pattern Description Example

(abc) Matches and captures the group "abc". (abc)+ matches "abcabc".

\1 Refers to the first captured group in the (a)(b)\1 matches "aba".


pattern.
(?:abc) Matches "abc" but doesn't capture it. (?:abc)+ matches "abcabc".

(?P<name>a Captures group with a name. (?P<word>\w+) matches


bc) "word".

6. Lookahead and Lookbehind


Lookarounds allow you to match based on surrounding text without including it in the match.

Pattern Description Example

(?=abc Positive lookahead (matches if "abc" follows). a(?=b) matches "a" in


) "ab".

(?!abc Negative lookahead (matches if "abc" does not a(?!b) matches "a" in
) follow). "ac".

(?<=ab Positive lookbehind (matches if "abc" precedes). (?<=a)b matches "b" in


c) "ab".

(?<!ab Negative lookbehind (matches if "abc" does not (?<!a)b matches "b" in
c) precede). "cb".

7. Special Sequences
Special sequences simplify certain tasks.

Pattern Description Example

` ` Alternation (matches either the left or right).

\ Escapes special characters. \. matches a literal period.

\A Matches the start of the \Ahello matches "hello".


string.

\Z Matches the end of the string. world\Z matches "world".


8. Flags
Flags modify how the regular expression is interpreted.

Flag Description Example

re.IGNORECASE Makes the match re.match('abc', 'ABC',


(re.I) case-insensitive. re.I)

re.DOTALL (re.S) Allows . to match newline re.match('a.*b', 'a\nb',


characters. re.S)

re.MULTILINE ^ and $ match start and end of re.match('^abc$',


(re.M) lines. 'abc\nabc', re.M)

You might also like