Cheatography
https://cheatography.com
Regular Expressions that I have been taught.
Definition
Regular expression is a tool for matching patterns in text. With the help of regular expression you can find, match and replace text in strings. |
A regular expression is a special sequence of characters that helps you match or find other strings or sets of strings, using a specialized syntax held in a pattern. |
Module name re is imported to be used. |
White Space Characters
\n |
Newline |
\s |
Space |
\t |
Tab |
\e |
Escape |
\r |
Return |
|
|
Identifiers
\d |
Any single character or number |
\D |
Anything but a number (non-digit character) |
\s |
Space |
\S |
Anything but a space |
\w |
Any character (A-Z, a-z, 0-9 and underscore) |
\W |
Anything but a character |
. |
Any single character except for a new line |
\b |
The white space around words |
\. |
A period |
Functions
match() |
Syntax - re.match(pattern, string, <flag=0>) |
search() |
For any pattern in the string - only the first match |
findall() |
Matches all occurrences |
split() |
Splits the string - forms an array |
replace() |
To replace one or more characters in a string |
|
|
Modifiers
{x,y} |
We expect x to y integers |
+ |
Match one or more occurrences |
? |
Match 0 or 1 occurrence |
* |
Match 0 or more of the preceding expression (eg. x* will match occurrences of x) |
$ |
Match the end of the string |
^ |
Matching the beginning of a string |
| |
Used as OR |
[] |
Displaying a range |
() |
To group the regular expressions |
|
Created By
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets