Python regular expression tutorial : learn regular expression in python for beginners and Findall () function, Search () function, Split function () and Sub () function. The regular expression in the programming language is a special string used for a search pattern. It is extremely used for extracting the information from text code, files, log, and documents. The first thing while using the expression is to recognize everything essential and writing pattern. The ascii or latin letters are your keyboard and Unicode which is used to match the text.
They will include the punctuation and digits with special characters like $%@!etc.
It is a sequence of characters that will search a pattern and used to check the string that will contain search patterns.
The regular expression is called tiny and special programming language inside python through the re module.
The patterns are compiled into series and executed by matching the engine which is written in c.
This expression is small and does not allow task using the regular expression.
They will help to match or find the set of strings of specified syntax and held the pattern.
Also used in the UNIX world and‘re’ provides full support for Perl.
Various characters are used in a regular expression.
To avoid confusion the deal is with a regular expression as r expression.
1 |
Pattern:-it is the regular expression to match. |
2 |
String:-it is used to search and match the pattern at the beginning of a string. |
3 |
Flags:-you specify flags using the bitwise OR and modifiers. |
It is built-in a package called re and used to work with regular expression.
Import the re module,
Import re
regEx functions:-
The re module which offers the set of functions and allow us to search the string,
function |
description |
findall |
It returns the list containing all matches |
Search |
It returns if the match is present in a string |
sub |
Replaces one or more matches with the string |
They are called special meaning characters.
character |
description |
[] |
A set of charters |
\ |
The special sequence signals |
. |
The character of any type |
^ |
Stars with |
$ |
Ends with |
* |
Zero or more occurrences |
Set: it is the character inside a pair of square brackets [] with special meaning.
set |
description |
[arn] |
It returns a match where one of the specified characters is present. |
[a-n] |
It matches any lower case character between a and n. |
[^arn] |
It return a match for any character except a,r. |
It will return the list containing all matches.
Example:-
Print the list of matches,
Import re
Txt=”the rain in pune”
X=re.findall (“ai”, txt)
Print(x)
If the match us found if it contains in a list.
If not found then the empty list is returned.
This function will search the string for match and return a match object if the match exists.
Example:-
Import re
Txt=”The rain in pune”
X=re.search (“\s”,txt)
Print(“The first white-space character is located in position:”,x.start())
This function will return the list where the string has split by match.
Example:-
Import re
Txt=”the rain in spain”
X=re.split (“/s”, txt)
Print(x)
You can control several occurrences by specifying maxsplit parameter.
This will replace the match with the text of your choice.
Example:-
Replace the white space character with number 9.
Import re
Txt=”the rain in spain”
X=re.sub (“/s”, “9”, txt)
Print(x)
It will control replacement by specifying the count parameter.