re.match()
match returns an object representing the match, if not, it returns None. Sub()
This method replaces all occurrences of the pattern in string with repl, substituting all occurrences, unless count provided. This method returns the modified string. ^start &end
The next two metacharacters are ^ and $. These match the start and end of a string, respectively. [] character classes 3
special sequences
There are various special sequences you can use in regular expressions. They are written as a backslash followed by another character. One useful special sequence is a backslash and a number between 1 and 99, e.g., \1 or \17. This matches the expression of the group of that number. ? metacharacter
The metacharacter ? means "zero or one repetitions". {} metacharacters
Curly braces can be used to represent the number of repetitions between two numbers. The regex {x,y} means "between x and y repetitions of something". Hence {0,1} is the same thing as ?. If the first number is missing, it is taken to be zero. If the second number is missing, it is taken to be infinity. |
search() and findall()
The function re.search finds a match of a pattern anywhere in the string. The function re.findall returns a list of all substrings that match a pattern. . (dot).
This matches any character, other than a new line. \d \s \w Special sequences
More useful special sequences are \d, \s, and \w. These match digits, whitespace, and word characters respectively. In ASCII mode they are equivalent to [0-9], [ \t\n\r\f\v], and [a-zA-Z0-9_]. In Unicode mode they match certain other characters, as well. For instance, \w matches letters with accents. Versions of these special sequences with upper case letters - \D, \S, and \W - mean the opposite to the lower-case versions. For instance, \D matches anything that isn't a digit. [] character classes 2
Character classes can also match ranges of characters. The class [a-z] matches any lowercase alphabetic character. The class [G-P] matches any uppercase character from G to P. The class [0-9] matches any digit. Multiple ranges can be included in one class. For example, [A-Za-z] matches a letter of any cases. + metacharacter
The metacharacter + is very similar to *, except it means "one or more repetitions", as opposed to "zero or more repetitions". Groups in metacharacters ()
The content of groups in a match can be accessed using the group function. A call of group(0) or group() returns the whole match. A call of group(n), where n is greater than 0, returns the nth group from the left. The method groups() returns all groups up from 1. |
\A \Z \b special sequences
Additional special sequences are \A, \Z, and \b. The sequences \A and \Z match the beginning and end of a string, respectively. The sequence \b matches the empty string between \w and \W characters, or \w characters and the beginning or end of the string. Informally, it represents the boundary between words. The sequence \B matches the empty string anywhere else. | "or" metacharacter
Another important metacharacter is |. This means "or", so red|blue matches either "red" or "blue". named groups & noncapturing groups
Named groups have the format (?P<name>...), where name is the name of the group, and ... is the content. They behave exactly the same as normal groups, except they can be accessed by group(name) in addition to its number. Non-capturing groups have the format (?:...). They are not accessible by the group method, so they can be added to an existing regular expression without breaking the numbering. * metacharacter
The metacharacter * means "zero or more repetitions of the previous thing". [] character classes
Character classes provide a way to match only one of a specific set of characters. Search->>Group, Start,End,Span
The regex search returns an object with several methods that give details about it. These methods include group which returns the string matched, start and end which return the start and ending positions of the first match, and span which returns the start and end positions of the first match as a tuple. |
Cheatography
https://cheatography.com
python regex(regular expression) Cheat Sheet by nimakarimian
regular expression using python 3
Created By
https://www.nimakarimian.ir
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by nimakarimian