The concept of regular expressions in Python
In the beginning, regular expressions are calledRegular ExpressionsorRegExThey are texts that contain letters and symbols that have specific meanings.
To be more precise, every letter or symbol we put in this text means a specific thing.
The concept of regular expressions in Python.
In the beginning, regular expressions are called Regular Expressions or RegEx, which are texts that contain letters and symbols that have specific meanings. To be more precise, every letter or symbol we put in this text means something specific.
So, regular expressions are used in order to search the texts in a very easy way instead of having to write complex algorithms in order to reach the desired result.
Thus, you can take advantage of regular expressions if you want to search in the text for a specific thing in order to make an amendment or update on it.
So, regular expressions are used for the purpose of searching texts in a very easy way instead of having to write complex algorithms in order to reach the desired result. Thus, you can use regular expressions in case you want to search in the text for something specific with the aim of modifying or updating it.
One of the most popular uses of regular expressions is when you ask the user to enter their email address and password.
Then you find that while typing the e-mail and password, it shows you alerts in case you did not enter a correct e-mail or a password of a certain form.
For example, you will find that you are required to put in a password that consists of at least eight characters and must contain an uppercase letter, a lowercase letter, a number and a symbol.
Dealing with regular expressions
reIt is a ready-made module in Python that contains ready-made functions and constants dedicated to handling regular expressions.
That's why you should include this module when you want to work with regular expressions.
To embed the modulere we write the following line.
import re
Module functionsre
After including the module re, we will be able to use the functions in it, the most important of which are mentioned in the following table.
| Function name and definition | |
|---|---|
search(pattern, string, flags=0)
You search in the text that we pass to it in place of the parameterstring to see if it matches or has a part that matches the regular expression we pass in that place parameterpattern.If one or more parts of the text are found that match the regular expression, it returns an object from the class Matchcontaining the information of the first place in that text that matches the regular expression. If the text to be searched is not found, it will be returned None.see example » |
|
findall(pattern, string, flags=0)
You look in the text we pass in that has the parameter's placestring to see if it matches or has a part that matches the regular expression we pass in that place parameterpattern.If one or more parts of the text that match the regular expression are found, listeach element in it is returned representing the part that matches the regular expression. If no matches are found, listnull is returned.see example » |
|
finditer(pattern, string, flags=0)
You look in the text we pass in that has the parameter's placestring to see if it matches or has a part that matches the regular expression we pass in that place parameterpattern.If one or more parts of the text that match the regular expression are found, iteratoreach element in it returns an object Matchrepresenting the part that matches the regular expression. If no matches are found, listnull is returned.see example » |
|
split(pattern, string, maxsplit=0, flags=0)
Returns a copy of the text we are passing in that of the parameter stringdivided into an array( list)of texts. The location of the parameter We patternpass a regular expression that specifies the way the text is divided and each section is placed in a single element within the array. If the text to be searched is not found, it returns lista single item containing all the searched text. Parameter position maxsplitYou can pass a number specifying how many sections you want the text to be divided into.see example » |
|
sub(pattern, repl, string, count=0, flags=0)
Returns a copy of the text we pass in the place of the parameter stringwith the replacement of each part in it matches the regular expression we pass to it in the place of the parameter with the patterntext we pass it in the place of the parameterrepl.Parameter location countYou can pass a number greater than zero representing the first how many fractions are found that you want to change.see example » |
|
Characters in Python
Featured Characters(Special Characters)They are symbols that have a special meaning and we have mentioned them in the following table.
| code | use it | Example |
|---|---|---|
^ |
It is used to check the characters placed at the beginning of each line in the text. Example: "^Hello"This expression means whether the text or every line in it begins with a word Hello. |
see example » |
$ |
Used to check the characters placed at the end of each line of text. Example: "World$"This expression means whether the text or every line in it ends with the word Hello. |
see example » |
. |
Used to indicate that any character must be present except for the character /nthat indicates the end of a line. Example: "He..o"This expression means whether there are Hetwo letters after it, and after it there is a letter o. |
see example » |
+ |
Used to find out if the character placed before it is present at least once in the text. Example: "ello+"This expression means whether there is one or more ellletters after it.o |
see example » |
* |
It is used when it is not considered whether the letter placed before it is present once, several times or not at all in the text. Example: "ello*"This expression means whether there is ell, and it does not matter if there are one oor more letters after it. |
see example » |
? |
It is used to find out if the character placed before it is present once or not in the text. Example: "ai?"This expression is intended to be afollowed by one letter i. |
see example » |
| |
Used to find out if the text placed before it or the text placed after it is in the text. Example: "Hello|Hi"This expression means is there HelloorHi. |
see example » |
{m} |
Used to find out if the prefixed character is in the text a specified number of times. Place the character mwe pass the number of times. Example: "al{2}"This expression is meant to be afollowed by two lettersl. |
see example » |
{m,n} |
It is used to find out if the letter placed before it is present in the text a number of times between two numbers. The place of the letter mwe pass the number of times the least, and the place of the letter nwe pass the number of times the most. Example: "al{1, 2}"This expression means is there? a followed by one or two lettersl. |
see example » |
A mode setof possibilities in Python
The intent here is that you put the symbol []and inside it pass a set of characters and other symbols as we did in the following table.
| code | use it | Example |
|---|---|---|
[abc] |
This expression means whether there is in the text the letter aor borc. |
see example » |
[^abc] |
This expression means is there any letter in the text other than the letters aand bandc. |
see example » |
[a-f] |
This expression means whether there is in the text one of the letters of the alphabet between the letter aand the letterf.Here as we said: [abcdef]. |
see example » |
[0123] |
This expression is meant whether there is in the text the number 0or 1or 2or3. |
see example » |
[0-9] |
This expression means Is there in the text any of the numbers between the number 0and the number ?9.Here as we said: [0123456789]. |
see example » |
[0-5][0-9] |
This expression means Are there any two numbers between 00and . in the text59. |
see example » |
[a-zA-Z] |
This expression means whether there is in the text one of the letters of the alphabet between aandz,or between AandZ.That is, as if you are trying to find out if there is any letter of the alphabet, whether the letter is uppercase(Capital Letter)or small(Small Letter). | see example » |
[+] |
This expression means Is there a symbol in the text?+.That is, this symbol is treated here as a normal letter and this applies to the rest of the other symbols such as *, ., |, $, ), (, {, }. | see example » |
[a\-z] |
This expression means whether there is a letter a, symbol -or letter zin the text. So, here the symbol \makes the letter that is placed after it treated as a normal letter and not a symbol that has a special meaning. | see example » |
[a-][-a] |
Both expressions mean whether the letter aor symbol is present -in the text. So, when the symbol is not placed -between two letters or numbers, it is treated as a normal letter and not a symbol that has a special meaning. | see example » |
[^^] |
This expression means whether there is any letter or symbol in the text except for the symbol^. | see example » |
Featured Strings in Python
Featured Strings(Special Sequences)It is a set of characters that has a special meaning when it is placed after the symbol /and we have mentioned them in the following table.
| code | use it | Example |
|---|---|---|
\w |
Matches any character between a-zor A-Zor any number between 0-9or symbol_.This symbol is an abbreviation for Regular Expression [a-zA-Z0-9_]. |
see example » |
\W |
Matches any characters that are not between a-zor A-Z, number, or symbol_.This symbol is an abbreviation for Regular Expression [^a-zA-Z0-9_]. |
see example » |
\d |
Matches any character representing a number between 0and9.This symbol is an abbreviation for Regular Expression [0-9]. |
see example » |
\D |
Matches any characters that are not a number between 0and9.This symbol is an abbreviation for Regular Expression [^0-9]. |
see example » |
\s |
Matches any character that represents a blank space. This symbol is an abbreviation for Regular Expression [ \t\n\r\f\v]. |
see example » |
\S |
Matches any character that is not a blank space. This symbol is an abbreviation for Regular Expression [^ \t\n\r\f\v]. |
see example » |
\Z |
Matches any string we pass before it with the characters at the end of the text. | see example » |
\A |
It matches any character string we pass after with the characters at the beginning of the text. | see example » |
\b |
Matches the first or the last of any character string that contains between characters a-zor A-Zany between number 0-9or the symbol_.
|
see example » |
\B |
Does not match the first or the last of any character string containing between characters a-zor A-Zany between number 0-9or the symbol_.
|
see example » |
Whatch out
The symbol \is called .Backslash,and heEscape CharacterAny character has a special meaning in Python as you noted in the previous table.
If you want to search for the \same character, you should know that the Python interpreter sees every character \, that is, it sees it\\.
And therefore you will have to put \\\\whenever you mean \so that the Python interpreter understands that you want to search for this character and not mean anything else.
If you don't want to, you can simply adopt a style of being toldRaw String,That is, you put the character rimmediately before the regular expression text as followsr"\Bea".
Of course, you could have written the previous expression like this, too"\\Bea".
In the end, we advise you to adopt a methodtheRaw StringBecause it is easier and less complicated.
object Matchin python
The object Matchreturned by some module functions realso provides you with four basic functions for working with the information it contains.
| Function name and definition | |
|---|---|
group()
Returns the string of characters found in the text and stored in an objecttheMatch()who summoned her. |
|
start()
Returns an integer representingIndexThe first character in a string that is found in the text and stored in an objecttheMatch()who summoned her. |
|
end()
Returns an integer representingIndexThe last character in a string that was found in the text and stored in an objecttheMatch()who summoned her. |
|
span()
Stored Returns tuplewhere the string stored in an object was foundtheMatch()who summoned her. thisthe tupleIt contains only two numbers. The first number represents .IndexThe first letter has a match, the second number representsIndexThe last letter found has a match. |
|
Here we have put a comprehensive example using the four functions mentioned in the table.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python is an easy language to learn"
# Finally, the search result will be returned. 'e' will search the text for the first word you find that begins with the search() function
# If no match is found, if you find any word that matches the regular expression or Match as an object of the class
result = re.search(r"\be\w+", txt)
# if the print commands placed in the -Match clause will be executed because it contains an object - None that does not contain the value result since the object
if result:
print(result.string) # Here we have printed the text we searched in
print(result.group()) # Here we have printed the word that matched the regular expression
print(result.start()) # The first character where the match is found Index Here we have printed
print(result.end()) # Last character where the match was found Index Here we have printed
print(result.span()) # The first and last character then the match is found Index Here we have printed
else:
print("No match found!")
• We will get the following result when we run the fileTest.
easy
13
17
(13, 17)
Python functionsearch()
its definition
You look in the text we pass in that has the parameter's place stringto see if it matches or has a part that matches the regular expression we pass in that place parameterpattern.
If one or more parts of the text are found that match the regular expression, it returns an object from the class Matchcontaining the information of the first place in that text that matches the regular expression.
If the text to be searched is not found, it will be returnedNone.
built
search(pattern, string, flags=0)
parameters
The location of the parameter
stringwe pass in which we want to search.Parameter location We
patternpass a regular expression representing the text to be searched.
Return value
If one or more parts of the text are found that match the regular expression, it returns an object from the class Matchcontaining the information of the first place in that text that matches the regular expression.
If the text to be searched is not found, it will be returnedNone.
In the following example, the regular expression ^Python.*learn*means: Does the text begin with a word Pythonand end with a word ?learn.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python is an easy language to learn"
# Match has become an object of the class result, it means that learn and ends with the word Python since the text begins with the word
result = re.search("^Python.*learn?", txt)
# if the print function enclosed in the -match clause will be executed because it contains an object - None that does not contain the value result since the object
if result:
print("Yes, We found Match!")
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Python functionfindall()
its definition
You look in the text we pass in that has the parameter's place stringto see if it matches or has a part that matches the regular expression we pass in that place parameterpattern.
If one or more parts of the text that match the regular expression are found, listeach element in it is returned representing the part that matches the regular expression.
If no matches are found, listnull is returned.
built
findall(pattern, string, flags=0)
parameters
The location of the parameter
stringwe pass in which we want to search.Parameter location We
patternpass a regular expression representing the text to be searched.
Return value
If one or more parts of the text that match the regular expression are found, listeach element in it is returned representing the part that matches the regular expression.
If no matches are found, listnull is returned.
In the following example, the regular expression \w*ea\w*means: Is there an English word that contains the two lettersea.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python is an easy language to learn"
# contains the found words list becomes result, it means that ea since the text contains more than one English word in which the two letters
result = re.findall("\w*ea\w*", txt)
# and then print the result that passes every value in the non-empty if object, the content of the statement list representing the result object will be executed since
if result:
for word in result:
print(word)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
learn
Python functionfinditer()
its definition
You look in the text we pass in that has the parameter's place stringto see if it matches or has a part that matches the regular expression we pass in that place parameterpattern.
If one or more parts of the text that match the regular expression are found, iteratoreach element in it returns an object Matchrepresenting the part that matches the regular expression.
If no matches are found, listnull is returned.
built
finditer(pattern, string, flags=0)
parameters
The location of the parameter
stringwe pass in which we want to search.Parameter location We
patternpass a regular expression representing the text to be searched.
Return value
If one or more parts of the text that match the regular expression are found, iteratoreach element in it returns an object Matchrepresenting the part that matches the regular expression.
If no matches are found, listnull is returned.
In the following example, the regular expression \w*ea\w*means: Is there an English word that contains the two lettersea.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python is an easy language to learn"
# result It means that ea since the text has more than one English word with the two letters
# It has all the details of the word that was found Match Every element in it represents an iterator object has become
result = re.finditer("\w*ea\w*", txt)
# and then print result which is passing every value in the non-empty if object, the content of the iterator statement representing the result object will be executed since
if result:
for word in result:
print(word)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
<_sre.SRE_Match object; span=(30, 35), Match='learn'>
Python functionsplit()
its definition
Returns a copy of the text we are passing in that of the parameter stringdivided into an array( list)of texts.
built
split(pattern, string, maxsplit=0, flags=0)
parameters
The location of the parameter
stringwe pass in which we want to search.The location of the parameter We
patternpass a regular expression that specifies the way the text is divided and each section is placed in a single element within the array.Parameter position
maxsplitYou can pass a number specifying how many sections you want the text to be divided into.
Return value
Returns a copy of the text we are passing in that of the parameter stringdivided into an array( list)of texts.
If the text to be searched is not found, it returns lista single item containing all the searched text.
In the following example, the regular expression \smeans an empty space.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python is an easy language to learn"
# result of the words and then stored in the list object based on the empty spaces. This means that a txt will be generated here. We have split the text of the variable
result = re.split("\s", txt)
# and then print it result here we have passed over each value in the object
for word in result:
print(word)
• We will get the following result when we run the fileTest.
is
an
easy
language
to
learn
Python functionsub()
its definition
Returns a copy of the text we pass in the place of the parameter stringwith the replacement of each part in it matches the regular expression we pass to it in the place of the parameter with the patterntext we pass it in the place of the parameterrepl.
built
sub(pattern, repl, string, count=0, flags=0)
parameters
The location of the parameter
stringwe pass in which we want to search.Parameter location We
patternpass a regular expression representing the text to be searched.The location of the parameter
replWe pass the text we want to put in place of the searched text, if it is found.Parameter location
countYou can pass a number greater than zero representing the first how many fractions are found that you want to change.
Return value
Returns a copy of the text we pass in the place of the parameter stringwith the replacement of each part in it matches the regular expression we pass to it in the place of the parameter with the patterntext we pass it in the place of the parameterrepl.
In the following example, the regular expression \smeans an empty space.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python is an easy language to learn"
# result Replace the first three blank spaces in it with the symbol '-' and then store this text in the variable txt A copy of the text in the variable sub() here the function will return
result = re.sub("\s", '-', txt, 3)
# result Here we have printed the text that was stored in the variable
print(result)
• We will get the following result when we run the fileTest.
Symbol ^in regular expressions
Remember: the symbol ^is used to check the characters placed at the beginning of each line of text.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Hello World"
# in None and returns 'Hello' , if the text begins with the word Match returns the search() object of the function
result = re.search("^Hello", txt)
# if The print commands in the 'Hello' sentence where the text begins with Match is a result object will be executed since
if result:
print("The string is starts with 'Hello'.")
print('Returned Match object', result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Returned Match object <_sre.SRE_Match object; span=(0, 5), Match='Hello'>
Python symbol $in regular expressions
Remember: the symbol $is used to check the characters placed at the end of each line of text.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Hello World"
# if it is not None and returns 'World' , if the text ends with the word Match returns the object of the search() function
result = re.search("World$", txt)
# if the print commands in the 'World' sentence where the text ends with Match representing a result object will be executed since
if result:
print("The string is ends with 'World'.")
print('Returned Match object', result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Returned Match object <_sre.SRE_Match object; span=(6, 11), Match='World'>
Python symbol .in regular expressions
Remember: the symbol .- the regular period - is used to indicate that any character must be present except for the character /nthat indicates the end of a line.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Hello World"
# list will return the search result as an object of type 'o' followed by two random characters, ending with 'He' will search for every word that begins with the findall() function
result = re.findall("He..o", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Python symbol +in regular expressions
Remember: the symbol +is used to find out if the character placed before it is present at least once in the text.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "I like potato and tomato"
# list or more. In the end it will return the search result as an object of type 't' followed by the letter 'a' It will search for every character the findall() function
result = re.findall("at+", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Remember: the symbol *is used when you don't care whether the letter placed before it is present once, many times or not at all in the text.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "I like potato and tomato"
# list or more. In the end it will return the search result as an object of type 't' followed by a letter and it doesn't matter if there is a letter 'a' after it, it will search for every letter findall()
result = re.findall("at*", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Python symbol ?in regular expressions
Remember: the symbol ?is used to find out if the character placed before it is present once or not in the text.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "I like potato and tomato"
# list or more. In the end it will return the search result as an object of type 't' followed by the letter 'a' It will search for every character the findall() function
result = re.findall("at?", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Remember: the symbol |is used to tell if the text placed before it or the text placed after it is in the text.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Hello sister.. Hi brother"
# list At the end it will return the search result as an object of type 'Hi' and 'Hello' will search for every findall() function
result = re.findall("Hello|Hi", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Python symbol {m}in regular expressions
Remember: a symbol {m}is used to find out if the character placed before it is in the text a specified number of times.
Place the character mwe pass the number of times.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Lina, as a beginner you should study algorithms"
# list At the end the search result will be returned as an object of type 'n' followed by two 'i' characters. The findall() function will search for each character.
result = re.findall("in{2}", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Python symbol {m,n}in regular expressions
Remember: the symbol {m,n}is used to find out if the character before it is in the text a number of times between two numbers.
The place of the letter mwe pass the number of times the least, and the place of the letter nwe pass the number of times the most.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Lina, as a beginner you should study algorithms"
# list At the end the search result will be returned as an object of type 'n' followed by one or two 'i' characters. The findall() function will search for each character
result = re.findall("in{1,2}", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression [abc]in Python
Remember: the expression [abc]means whether there is a letter aor bor . in the textc.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "No pain, no gain"
# list At the end it will return the search result as an object of type 'c', 'b' or 'a' will search the text for each character of the findall() function
result = re.findall("[abc]", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression [^abc]in Python
Remember: the expression [^abc]means whether there is any letter in the text other than the letters aand bandc.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "No pain, no gain"
# list At the end it will return the search result as an object of type .'c', 'b' and 'a' will search the text for any character except the characters findall() function
result = re.findall("[^abc]", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression [a-f]in Python
Remember: the expression [a-f]means whether there is in the text one of the letters of the alphabet between the letter aand the letterf.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Alphabet are easy to remember"
# list At the end it will return the search result as an object of type .'f' and 'a' will search the text for any character between the two characters findall() function
result = re.findall("[af]", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression [0123]in Python
Remember: the expression [0123]means whether there is in the text the number 0or 1or 2or3.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "If you have an array of 4 elements. This means that first index in it is 0 and last index is 3"
# list . will search the text for every number '0', '1', '2' and '3'. Finally, the search result will be returned as an object of type findall() function
result = re.findall("[0123]", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression [0-9]in Python
Remember: the expression [0-9]is intended to be intended. Is there one of the numbers between the number 0and the number in the text?9.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "This tutorial is written in 2019"
# list . It will search the text for every number between '0' and '9'. Finally, the search result will be returned as an object of type findall() function
result = re.findall("[0-9]", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression [0-5][0-9]in Python
Remember: the expression [0-5][0-9]means Are there any two numbers between 00and . in the text59.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "60 will not Match, but 07 and 59 will be Matched"
# list . It will search the text for every number between '00' and '59'. Finally, the search result will be returned as an object of type findall() function
result = re.findall("[0-5][0-9]", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression [a-zA-Z]in Python
Remember: the expression [a-zA-Z]means whether there is in the text one of the letters of the alphabet between aandz,or between AandZ.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Today, last version of Python is 3.7.2"
# list At the end it will return the search result as an object of type 'Z' and 'A' and between 'z' and 'a' Searches the text for each alphabetic character between the findall() function
result = re.findall("[a-zA-Z]", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
[+]in Python
Remember: the expression [+]means Is there a symbol in the text?+.
That is, this symbol is treated here as a normal letter and this applies to the rest of the other symbols such as*, ., |, $, ), (, {, }.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "1 + 3 = 5"
# list . It will search the text for every '+', every '=', and every number between '0' and '9'. Finally, the search result will be returned as an object of type findall() function
result = re.findall("[+=0-9]", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression [a\-z]in Python
Remember: the expression [a\-z]means whether the letter a, symbol -or letter is present zin the text.
So, here the symbol \makes the letter that is placed after it treated as a normal letter and not a symbol that has a special meaning.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "a - b = c"
# list At the end it will return the search result as an object of type 'z' and the '-' symbol and the letter 'a' will search the text for each character findall() function
result = re.findall("[a\-z]", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression [a-]and [-a]in Python
Remember: What is meant by both expressions is whether there is a letter [a-]or symbol in the text.
So, when the symbol is not placed between two letters or numbers, it is treated as a normal letter and not a symbol that has a special meaning.[-a]a--
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "a - b = c"
# list and the '-' symbol. In the end it will return the search result as an object of type 'a' It will search the text for every character findall() function
result = re.findall("[a-]", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression [^^]in Python
Remember: the expression [^abc]means whether there is any letter or symbol in the text except for the symbol^.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "5^2=10"
# list will search the text for any character or symbol except '^'. Finally, the search result will be returned as an object of type findall() function
result = re.findall("[^^]", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression \win Python
Remember: the expression \wmatches any character between a-zor A-Zor any number between 0-9or symbol_.
This symbol is an abbreviation of a regular expression[a-zA-Z0-9_].
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python 3 is amazing"
# list At the end it will return the search result as an object of type 'Z' and 'A' and between 'z' and 'a' Searches the text for each alphabetic character between the findall() function
result = re.findall("\w", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression \Win Python
Remember: the expression \Wmatches any characters that are not between a-zor A-Z, number, or symbol_.
This symbol is an abbreviation of a regular expression[^a-zA-Z0-9_].
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python 3 is amazing"
# list At the end it will return the search result as an object of type 'Z' and 'A' and between 'z' and 'a' Searches the text for each alphabetic character between the findall() function
result = re.findall("\W", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression \din Python
Remember: the expression \dmatches any character that represents a number between 0and9.
This symbol is an abbreviation of a regular expression[0-9].
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python 3"
# list . It will search the text for each character that represents a number between '0' and '9'. Finally, the search result will be returned as an object of type findall() function
result = re.findall("\d", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression \Din Python
Remember: the expression \Dmatches any characters that are not a number between 0and9.
This symbol is an abbreviation of a regular expression[^0-9].
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python 3"
# list . It will search the text for every character that is not a number between '0' and '9'. Finally, the search result will be returned as an object of type findall() function
result = re.findall("\D", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression \sin Python
Remember: the expression \smatches any character that represents a blank space.
This symbol is an abbreviation of a regular expression[ \t\n\r\f\v].
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python 3"
# list . will search the text for each character that represents a blank space. Finally, the search result will be returned as an object of type findall() function
result = re.findall("\s", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression \Sin Python
Remember: the expression \Smatches any character that is not a blank space.
This symbol is an abbreviation of a regular expression[^ \t\n\r\f\v].
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python 3"
# list . will search the text for every character that is not a blank space. Finally, the search result will be returned as an object of type findall() function
result = re.findall("\S", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression \Zin Python
Remember: the expression \Zmatches any string of characters we pass before it with the characters at the end of the text.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Hello World"
# None and if not match returns an object .'ld' searches the text to see if it ends with the search() function
result = re.search("ld\Z", txt)
# if the print command placed in the Match statement representing a result object will be executed since
if result:
print('Match found!')
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression \Ain Python
Remember: the expression \Amatches any string we pass after with the characters at the beginning of the text.
Example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Hello World"
# None and if not match returns an object .'ld' searches the text to see if it ends with the search() function
result = re.search("\AHe", txt)
# if the print command placed in the Match statement representing a result object will be executed since
if result:
print('Match found!')
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression \bin Python
Remember: the expression \bmatches the first or the last of any character string that contains the characters between a-zor A-Zor any number between 0-9or the symbol_.
If the expression is placed at the end of a string, it searches for a match at the end of each unbroken string with a blank space in the text.
If the expression is placed at the beginning of a string, it searches for a match in the first string of unbroken strings with a blank space in the text.
In the following example, we will place the symbol \bin the first regular expression.
Note that the character must be placed ras we did before the regular expression text so that the Python interpreter can distinguish between a symbol \band a few characters because we do not want it to treat the symbol \bas normal characters.
first example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python is an easy language to learn"
# list At the end the search result will be returned as an object of type 'ea'. It will search the text for every word that begins with the findall() function.
result = re.findall(r"\bea", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
In the following example, we will place the symbol \bat the end of the regular expression.
Note that the character must be placed ras we did before the regular expression text so that the Python interpreter can distinguish between a symbol \band a few characters because we do not want it to treat the symbol \bas normal characters.
second example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python is an easy language to learn"
# list At the end it will return the search result as an object of its type. 'n' will search the text for every word ending in the findall() function
result = re.findall(r"n\b", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
Regular expression \Bin Python
Remember: the expression \B does not match the first or the last of any character string containing the characters between a-zor A-Zor any number between 0-9or the symbol_.
If the expression is placed at the end of a string, it searches for a match that does not exist before the last of each unbroken string with a blank space in the text.
If the expression is placed at the first of a string, it searches for a match that does not exist after the first of each unbroken string in the text.
In the following example, we will place the symbol \Bin the first regular expression.
Note that the character must be placed ras we did before the regular expression text so that the Python interpreter can distinguish between a symbol \Band a few characters because we do not want it to treat the symbol \Bas normal characters.
first example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python is an easy language to learn"
# list but does not end with them. At the end it will return the search result as an object of type 'ea' You will search the text for every word containing the two letters findall() function
result = re.findall(r"\Bea", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.
In the following example, we will place the symbol \Bat the end of the regular expression.
Note that the character must be placed ras we did before the regular expression text so that the Python interpreter can distinguish between a symbol \Band a few characters because we do not want it to treat the symbol \Bas normal characters.
second example
# So that we can use the functions in it re here we have included the module
import re
# Here we have defined the text that we will look into shortly
txt = "Python is an easy language to learn"
# list but does not end with it. In the end it will return the search result as an object of type 'n' It will search the text for every word containing the character findall() function
result = re.findall(r"n\B", txt)
# is not empty, its contents will be printed as list is a result object since
if result:
print(result)
else:
print("No match found!")
• We will get the following result when we run the fileTest.