0% found this document useful (0 votes)
15 views2 pages

Regex Reference

Uploaded by

shaikatpaul98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Regex Reference

Uploaded by

shaikatpaul98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Regular Expressions:

List of all metacharacters: . ^ $ * + ? ] [ \ | ( ) { }


Operator Description
. Matches any character except \n

\ Escapes metacharacters

| Matches expression on either side of expression; has lowest priority of any operator

\d, \w, \s Predefined character group of digits (0-9), alphanumerics (a-z, A-Z, 0-9, and underscore), or
whitespace, respectively

\D, \W, \S Inverse sets of \d, \w, \s, respectively

* Matches preceding character/group zero or more times

? Matches preceding character/group zero or one times

+ Matches preceding character/group one or more times

*?, +? Applies non-greedy matching to * and +, respectively

{m} Matches preceding character/group exactly m times

{m, n} Matches preceding character/group at least m times and at most n times; if either m or n are
omitted, set lower/upper bounds to 0 and ∞, respectively

^, $ Matches the beginning and end of the line, respectively

[ ] Matching group used to match any of the specified characters or range (e.g. [abcde]) [a-e])

( ) Capturing group used to create a sub-expression

[^ ] Invert matching group; e.g. [^a-c] matches all characters except a, b, c

Regex String Matching:


Function Description
re.match(pattern, string) Returns a match if zero or more characters at beginning
of string matches pattern, else None
re.search(pattern, string) Returns a match if zero or more characters anywhere
in string matches pattern, else None
re.findall(pattern, string) Returns a list of all non-overlapping matches
of pattern in string (if none, returns empty list)
re.sub(pattern, repl, string) Returns string after replacing all occurrences
of pattern with repl
Data 100 Regular Expressions
(Spring 2022)
Here’s a complete list of metacharacters:

. ^ $ * + ? { } [ ] \ | ( )

Some reminders on what each can do (this is not exhaustive):

"^" matches the position at the beginning of string "\d" match any digit character. "\D" is the comple-
(unless used for negation "[^]") ment.
"$" matches the position at the end of string char- "\w" match any word character (letters, digits, un-
acter. derscore). "\W" is the complement.
"?" match preceding literal or sub-expression 0 or 1 "\s" match any whitespace character including tabs
times. and newlines. \S is the complement.
"+" match preceding literal or sub-expression one or "*?" Non-greedy version of *. Not fully discussed in
more times. class.
"*" match preceding literal or sub-expression zero or "\b" match boundary between words. Not discussed
more times in class.
"." match any character except new line.
"+?" Non-greedy version of +. Not discussed in
"[ ]" match any one of the characters inside, ac- class.
cepts a range, e.g., "[a-c]".
"{m,n}" The preceding element or subexpression
"( )" used to create a sub-expression must occur between m and n times, inclusive.

Some useful re package functions:

re.split(pattern, string) split the string at pattern to string replacing matching sub-
substrings that match the pattern. Returns strings with replace. Returns a string.
a list.
re.findall(pattern, string) Returns a list of all
re.sub(pattern, replace, string) apply the matches for the given pattern in the string.

You might also like