python regular expression cheat sheet- regex pattern matching

python regular expression cheat sheet

Welcome everyone , This post is regarding python regular expression cheat sheet ,regex pattern matching If you want to learn python crash course free then click on python tutorials point so let's start :
This blog posts gives an overview and example of regular expressions syntax as implemented by the re built-in module (Pythons 3.7). Assume ASCII characters sets unless otherwise specified. 
what is regular expression cheat sheet ?
A regular expressions specifies the sets of a strings that matches to it, the function in this modules let you check if a particular strings match a given regular expressions. python
python regular expression cheat sheet


python regular expression cheat sheet example :

  The following tabular form are provide detail about how to apply regular expression on character of a strings:

[.]

This tabular form Match any characters except the newline characters

[^]

This tabular form  Match the beginning of the strings provided.

[$]

This tabular form Match the end of the strings provided.

[*]

This tabular form Match 0 or more repetitions of the provided characters.

[+]

This tabular form Match 1 or more repetitions of the provided characters.

[?]

This tabular form Match either 0 or 1 repetition of a characters.

 

[*?]

This tabular form Match 0 or more repetitions of the provided characters

 in a non-greedy manners.

[+?]

This tabular form Match 1 or more repetitions of the provided character 

in a non-greedy manners.

[??]

This tabular form Match either 0 or 1 repetition of a character in a non-greedy 

manners.

 

Escapes a special characters

[]

This tabular form Match a set of character provided within the square bracket.

[a-z]

This tabular form Match any lower cased ASCII letter.

[lower-upper]

This tabular form Match a set of character from lower to upper cased ASCII letter.

[^]

This tabular form Match a set of character that are not in a sets.

A|B

This tabular form Match either regular expressions A or B in a non-greedy manners.

 

[m]

This tabular form Match exactly m copies of the characters specified.

[m,n]

This tabular form Match from m to n repetitions of the characters specified.

[,n]

This tabular form Match from 0 to n repetitions of the characters specified.

[m,]

This tabular form Match from m to infinite repetitions of the characters specified.

[m,n]?

 This tabular form Match from m to n repetition in a non-greedy manners.


The following tabular form provide the list of flag that are available for ready usage in the regular kind of expression.

[i]

The Flags to denote to ignore the case.

[L]

The Flags to denote that it is Locale dependent.

[m]

The Multi-line flags.

[s]

The Dot matches all flags.

[x]

The Verbose flags.

[u]

The  Matches Unicode characters classes.

The followings tabular form provide detail about how to apply regular expression on special sequences and altogether. 

[A]

The Match only at the beginning of the strings.

[b]

The Match an empty string, only at the beginning or end of a words.

[B]

The Match an empty string, only when it is NOT at the beginning or end of a words.

[d]

The Match digits same as [0-9].

[D]

The Match anything that is not a digit, same as [^0-9].

 

[s]

The Match white space characters, similar to the [tnrfv].

[S]

The Match non-white space characters, similar to the [^tnrfv].

[w]

The match Unicode word characters, similar to the [a-zA-Z0-9_].

[W]

The Match any character that is not a Unicode word character, similar to the [^a-zA-Z0-9_].

[Z]

The Match only at the end of the given strings.

The followings tabular form provide detail about the RE Method that are provided for us to uses:

re.compile(patterns, flags)

The Compiles a regular expressions with a pattern along with flag.

re.match(patterns, string)

The  Match the pattern only at the beginning of the strings.

re.search(patterns, string)

The  Match the pattern anywhere in the provided strings.

re.split(patterns, string)

The  Split the string by the occurrences of the provided patterns.

re.sub(patterns, str2, string)

The  Replaces the left-most non-overlapping occurrence of the provided pattern in the strings with str2.

 

re.fullmatch(patterns, string)

The  Match the provided pattern only if the whole strings match the regular expressions.

re.findall(patterns, string)

The  Returns all non-overlapping match of the provided pattern in strings as a list of string.

re.finditer(patterns, string)

The  Returns an iterator that yields match object over non-overlapping matches of the provided pattern in strings.

re.subn(patterns, str2, string)

The  Replaces the left most occurrences of the given pattern in strings with str2 and return a tuples.

re.purge().

The  Clears the regular expressions cache of Pythons.

The following tabular form provides details about the groups that we can readily use:

(match)

This is used to specify a groups on which a match can be retrieved laters.

(?:match)

Non capturing versions parenthesis, that is the match cannot be retrieved laters.

(?P)

Capture the groups with name “names”.

(?P=name)

Backtrack groups named “names” in same pattern.

(?#comment)

Denotes a comments.

The following tabular are form provide details about the match object that we can readily uses: 

match.groups(“name”)

the Return subgroups “name” of match.

match.groups()

the Return tuples containing all subgroup of matches.

match.groupdict()

the Return dict containing all named subgroup of matches.

match.start(groups)

the Return the start index of substring match by groups.

match.end(groups)

the Return the end index of substring match by groups.

match.span(groups)

the Return 2 tuple start and end indices of groups in matches.

 

[match.pos]

 The Values of pos which was passed either to search() or match().

[match.endpos]

The Values of endpos which was passed either to search() or match().

[match.lastindex]

The Integer index of last matched capturing groups.

[match.lastgroup]

The Name of the last matched capturing groups.

[match.re]

 regular expressions who match() or search() created the matchs.

[match.string]

 string that was passed to either match() and search().

 python regex Matching pattern :

Regular expression are complicated mini-languages. They are rely on special character to matches unknown string, but let's starts with literal character, such as letter, number, and the space characters, which always match themselves. 

python regex matching pattern


Let's see a basic examples:

# we are Need modules 're' for regular expressions
import re
#
search_strings = "pythonslearning"
patterns = "pythons"
matches = re.match(patterns, search_strings)
# This If-statement after search() test if it succeeded
if match:
   print("regex match: ", matches.group())
else:
   print('pattern not found or generate')

Result:

regex match: pythons

python regex replace : There are different way to regex  replace

Tag: python regular expression cheat sheet, python regex tester, python regex replace,python pattern matching,python regex extract 
for similar kind of post click on pythonslearning and if you like this post then please comment and share.
BEST OF LUCK!!!!
Previous
Next Post »

method vs function python-types of method-class in python

method vs function python : Welcome to my article, In this article we are going to see What is method vs function  in python. before going t...