-A RegEx, or Regular Expression, is a sequence of characters that forms a search pattern.
-RegEx can be used to check if a string contains the specified search pattern.
-Python have a build-in module 're' that are deals with the regular expression.
How import Regular expression module ?
import re
Regular expression functions-
match(): This function attempts to match RE pattern to string with optional flag.
search() :Returns a Match object if there is a match anywhere in the string.
findall() : Returns a list containing all matches.
split() : Returns a list where the string has been split at each match
sub() : Replaces one or many matches with a string
etc
Metacharacters
Metacharacters are characters with a special meaning:
[] A set of characters
Signals a special sequence (can also be used to escape special characters)
. Any character (except newline character)
^ Starts with
$ Ends with
* Zero or more occurrences
+ One or more occurrences
{} Exactly the specified number of occurrences
| Either or
() Capture and group
Special sequence-
Character Description
A Returns a match if the specified characters are at the beginning of the string
b Returns a match where the specified characters are at the beginning or at the end of a word
B Returns a match where the specified characters are present, but NOT at the beginning (or at the end) of a word
d Returns a match where the string contains digits (numbers from 0-9)
D Returns a match where the string DOES NOT contain digits
s Returns a match where the string contains a white space character
S Returns a match where the string DOES NOT contain a white space character
w Returns a match where the string contains any word characters (characters from a to Z, digits from 0-9, and the underscore _ character)
W Returns a match where the string DOES NOT contain any word characters
Z Returns a match if the specified characters are at the end of the string
Syntax that how to write a regular expression and match string.
re.match(pattren,string,flag=0)
re.findall(str1,str2)