The Wayback Machine - https://web.archive.org/web/20240927185109/https://www.geeksforgeeks.org/regular-expression-python-examples/
Open In App

Regular Expression (RegEx) in Python with Examples

Last Updated : 05 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A Regular Expression or RegEx is a special sequence of characters that uses a search pattern to find a string or set of strings.

It can detect the presence or absence of a text by matching it with a particular pattern and also can split a pattern into one or more sub-patterns.

Regex Module in Python

Python has a built-in module named “re” that is used for regular expressions in Python. We can import this module by using the import statement.

Example: Importing re module in Python 

Python
# importing re module
import re

How to Use RegEx in Python?

You can use RegEx in Python after importing re module.

Example:

This Python code uses regular expressions to search for the word “portal” in the given string and then prints the start and end indices of the matched word within the string.

Python
import re

s = 'GeeksforGeeks: A computer science portal for geeks'

match = re.search(r'portal', s)

print('Start Index:', match.start())
print('End Index:', match.end())

Output
Start Index: 34
End Index: 40

Note: Here r character (r’portal’) stands for raw, not regex. The raw string is slightly different from a regular string, it won’t interpret the \ character as an escape character. This is because the regular expression engine uses \ character for its own escaping purpose.

Before starting with the Python regex module let’s see how to actually write regex using metacharacters or special sequences. 

Metacharacters

Metacharacters are the characters with special meaning.

To understand the RE analogy, Metacharacters are useful and important. They will be used in functions of module re. Below is the list of metacharacters.

MetaCharacters

Description

\

Used to drop the special meaning of character following it

[]

Represent a character class

^

Matches the beginning

$

Matches the end

.

Matches any character except newline

|

Means OR (Matches with any of the characters separated by it.

?

Matches zero or one occurrence

*

Any number of occurrences (including 0 occurrences)

+

One or more occurrences

{}

Indicate the number of occurrences of a preceding regex to match.

()

Enclose a group of Regex

Let’s discuss each of these metacharacters in detail:

1. \ – Backslash

The backslash (\) makes sure that the character is not treated in a special way. This can be considered a way of escaping metacharacters.

For example, if you want to search for the dot(.) in the string then you will find that dot(.) will be treated as a special character as is one of the metacharacters (as shown in the above table). So for this case, we will use the backslash(\) just before the dot(.) so that it will lose its specialty. See the below example for a better understanding.

Example: 

The first search (re.search(r'.', s)) matches any character, not just the period, while the second search (re.search(r'\.', s)) specifically looks for and matches the period character.

Python
import re

s = 'geeks.forgeeks'

# without using \
match = re.search(r'.', s)
print(match)

# using \
match = re.search(r'\.', s)
print(match)

Output
<re.Match object; span=(0, 1), match='g'>
<re.Match object; span=(5, 6), match='.'>

2. [] – Square Brackets

Square Brackets ([]) represent a character class consisting of a set of characters that we wish to match. For example, the character class [abc] will match any single a, b, or c. 

We can also specify a range of characters using – inside the square brackets. For example, 

  • [0, 3] is sample as [0123]
  • [a-c] is same as [abc]

We can also invert the character class using the caret(^) symbol. For example, 

  • [^0-3] means any character except 0, 1, 2, or 3
  • [^a-c] means any character except a, b, or c

Example:

In this code, you’re using regular expressions to find all the characters in the string that fall within the range of ‘a’ to ‘m’. The re.findall() function returns a list of all such characters. In the given string, the characters that match this pattern are: ‘c’, ‘k’, ‘b’, ‘f’, ‘j’, ‘e’, ‘h’, ‘l’, ‘d’, ‘g’.

Python
import re

string = "The quick brown fox jumps over the lazy dog"
pattern = "[a-m]"
result = re.findall(pattern, string)

print(result) 

Output
['h', 'e', 'i', 'c', 'k', 'b', 'f', 'j', 'm', 'e', 'h', 'e', 'l', 'a', 'd', 'g']

3. ^ – Caret

Caret (^) symbol matches the beginning of the string i.e. checks whether the string starts with the given character(s) or not. For example –  

  • ^g will check if the string starts with g such as geeks, globe, girl, g, etc.
  • ^ge will check if the string starts with ge such as geeks, geeksforgeeks, etc.

Example:

This code uses regular expressions to check if a list of strings starts with “The”. If a string begins with “The,” it’s marked as “Matched” otherwise, it’s labeled as “Not matched”.

Python
import re
regex = r'^The'
strings = ['The quick brown fox', 'The lazy dog', 'A quick brown fox']
for string in strings:
    if re.match(regex, string):
        print(f'Matched: {string}')
    else:
        print(f'Not matched: {string}')

Output
Matched: The quick brown fox
Matched: The lazy dog
Not matched: A quick brown fox

4. $ – Dollar

Dollar($) symbol matches the end of the string i.e checks whether the string ends with the given character(s) or not. For example-

  • s$ will check for the string that ends with a such as geeks, ends, s, etc.
  • ks$ will check for the string that ends with ks such as geeks, geeksforgeeks, ks, etc.

Example:

This code uses a regular expression to check if the string ends with “World!”. If a match is found, it prints “Match found!” otherwise, it prints “Match not found”.

Python
import re

string = "Hello World!"
pattern = r"World!$"

match = re.search(pattern, string)
if match:
    print("Match found!")
else:
    print("Match not found.")

Output
Match found!

5. . – Dot

Dot(.) symbol matches only a single character except for the newline character (\n). For example –  

  • a.b will check for the string that contains any character at the place of the dot such as acb, acbd, abbb, etc
  • .. will check if the string contains at least 2 characters

Example:

This code uses a regular expression to search for the pattern “brown.fox” within the string. The dot (.) in the pattern represents any character. If a match is found, it prints “Match found!” otherwise, it prints “Match not found”.

Python
import re

string = "The quick brown fox jumps over the lazy dog."
pattern = r"brown.fox"

match = re.search(pattern, string)
if match:
    print("Match found!")
else:
    print("Match not found.")

Output
Match found!

6. | – Or

Or symbol works as the or operator meaning it checks whether the pattern before or after the or symbol is present in the string or not. For example –  

  • a|b will match any string that contains a or b such as acd, bcd, abcd, etc.

7. ? – Question Mark

The question mark (?) is a quantifier in regular expressions that indicates that the preceding element should be matched zero or one time. It allows you to specify that the element is optional, meaning it may occur once or not at all. For example,

  • ab?c will be matched for the string ac, acb, dabc but will not be matched for abbc because there are two b. Similarly, it will not be matched for abdc because b is not followed by c.

8.* – Star

Star (*) symbol matches zero or more occurrences of the regex preceding the * symbol. For example –  

  • ab*c will be matched for the string ac, abc, abbbc, dabc, etc. but will not be matched for abdc because b is not followed by c.

9. + – Plus

Plus (+) symbol matches one or more occurrences of the regex preceding the + symbol. For example –  

  • ab+c will be matched for the string abc, abbc, dabc, but will not be matched for ac, abdc, because there is no b in ac and b, is not followed by c in abdc.

10. {m, n} – Braces

Braces match any repetitions preceding regex from m to n both inclusive. For example –  

  • a{2, 4} will be matched for the string aaab, baaaac, gaad, but will not be matched for strings like abc, bc because there is only one a or no a in both the cases.

11. (<regex>) – Group

Group symbol is used to group sub-patterns. For example –  

  • (a|b)cd will match for strings like acd, abcd, gacd, etc.

Special Sequences

Special sequences do not match for the actual character in the string instead it tells the specific location in the search string where the match must occur. It makes it easier to write commonly used patterns.  

List of special sequences 

Special Sequence

Description

Examples

\A

Matches if the string begins with the given character

\Afor 

for geeks

for the world

\b

Matches if the word begins or ends with the given character. \b(string) will check for the beginning of the word and (string)\b will check for the ending of the word.

\bge

geeks

get

\B

It is the opposite of the \b i.e. the string should not start or end with the given regex.

\Bge

together

forge

\d

Matches any decimal digit, this is equivalent to the set class [0-9]

\d

123

gee1

\D

Matches any non-digit character, this is equivalent to the set class [^0-9]

\D

geeks

geek1

\s

Matches any whitespace character.

\s

gee ks

a bc a

\S

Matches any non-whitespace character

\S

a bd

abcd

\w

Matches any alphanumeric character, this is equivalent to the class [a-zA-Z0-9_].

\w

123

geeKs4

\W

Matches any non-alphanumeric character.

\W

>$

gee<>

\Z

Matches if the string ends with the given regex

ab\Z

abcdab

abababab

RegEx Functions

re module contains many functions that help us to search a string for a match.

Let’s see various functions provided by this module to work with regex in Python. 

FunctionDescription
re.findall()finds and returns all matching occurrences in a list
re.compile() Regular expressions are compiled into pattern objects
re.split() Split string by the occurrences of a character or a pattern.
re.sub() Replaces all occurrences of a character or patter with a replacement string.
re.escape()Escapes special character
re.search()Searches for first occurrence of character or pattern

Let’s see the working of these RegEx functions with definition and examples:

1. re.findall()

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found.

Finding all occurrences of a pattern 

This code uses a regular expression (\d+) to find all the sequences of one or more digits in the given string. It searches for numeric values and stores them in a list. In this example, it finds and prints the numbers “123456789” and “987654321” from the input string.

Python
import re
string = """Hello my Number is 123456789 and
            my friend's number is 987654321"""
regex = '\d+'

match = re.findall(regex, string)
print(match)

Output
['123456789', '987654321']

2. re.compile() 

Regular expressions are compiled into pattern objects, which have methods for various operations such as searching for pattern matches or performing string substitutions. 

Example 1:

The code uses a regular expression pattern [a-e] to find and list all lowercase letters from ‘a’ to ‘e’ in the input string “Aye, said Mr. Gibenson Stark”. The output will be ['e', 'a', 'd', 'b', 'e'], which are the matching characters.

Python
import re
p = re.compile('[a-e]')

print(p.findall("Aye, said Mr. Gibenson Stark"))

Output
['e', 'a', 'd', 'b', 'e', 'a']

Understanding the Output: 

  • First occurrence is ‘e’ in “Aye” and not ‘A’, as it is Case Sensitive.
  • Next Occurrence is ‘a’ in “said”, then ‘d’ in “said”, followed by ‘b’ and ‘e’ in “Gibenson”, the Last ‘a’ matches with “Stark”.
  • Metacharacter backslash ‘\’ has a very important role as it signals various sequences. If the backslash is to be used without its special meaning as metacharacter, use’\\’

Example 2: Set class [\s,.] will match any whitespace character,  ‘,’,  or, ‘.’ . 

The code uses regular expressions to find and list all single digits and sequences of digits in the given input strings. It finds single digits with \d and sequences of digits with \d+.

Python
import re
p = re.compile('\d')
print(p.findall("I went to him at 11 A.M. on 4th July 1886"))

p = re.compile('\d+')
print(p.findall("I went to him at 11 A.M. on 4th July 1886"))

Output
['1', '1', '4', '1', '8', '8', '6']
['11', '4', '1886']

Example 3:

The code uses regular expressions to find and list word characters, sequences of word characters, and non-word characters in input strings. It provides lists of the matched characters or sequences.

Python
import re

p = re.compile('\w')
print(p.findall("He said * in some_lang."))

p = re.compile('\w+')
print(p.findall("I went to him at 11 A.M., he \
said *** in some_language."))

p = re.compile('\W')
print(p.findall("he said *** in some_language."))

Output
['H', 'e', 's', 'a', 'i', 'd', 'i', 'n', 's', 'o', 'm', 'e', '_', 'l', 'a', 'n', 'g']
['I', 'went', 'to', 'him', 'at', '11', 'A', 'M', 'he', 'said', 'in', 'some_language']
[' ', ' ', '*', '*', '*', ' ', ' ', '.']

Example 4:

The code uses a regular expression pattern ‘ab*’ to find and list all occurrences of ‘ab’ followed by zero or more ‘b’ characters in the input string “ababbaabbb”. It returns the following list of matches: [‘ab’, ‘abb’, ‘abbb’].

Python
import re
p = re.compile('ab*')
print(p.findall("ababbaabbb"))

Output
['ab', 'abb', 'a', 'abbb']

Understanding the Output: 

  • Our RE is ab*, which ‘a’ accompanied by any no. of ‘b’s, starting from 0.
  • Output ‘ab’, is valid because of single ‘a’ accompanied by single ‘b’.
  • Output ‘abb’, is valid because of single ‘a’ accompanied by 2 ‘b’.
  • Output ‘a’, is valid because of single ‘a’ accompanied by 0 ‘b’.
  • Output ‘abbb’, is valid because of single ‘a’ accompanied by 3 ‘b’.

3. re.split() 

Split string by the occurrences of a character or a pattern, upon finding that pattern, the remaining characters from the string are returned as part of the resulting list. 

Syntax : 

re.split(pattern, string, maxsplit=0, flags=0)

The First parameter, pattern denotes the regular expression, string is the given string in which pattern will be searched for and in which splitting occurs, maxsplit if not provided is considered to be zero ‘0’, and if any nonzero value is provided, then at most that many splits occur. If maxsplit = 1, then the string will split once only, resulting in a list of length 2. The flags are very useful and can help to shorten code, they are not necessary parameters, eg: flags = re.IGNORECASE, in this split, the case, i.e. the lowercase or the uppercase will be ignored.

Example 1:

Splits a string using non-word characters and spaces as delimiters, returning words: ['Words', 'words', 'Words']. Considers apostrophes as non-word characters: ['Word', 's', 'words', 'Words']. Splits using non-word characters and digits: ['On', '12th', 'Jan', '2016', 'at', '11', '02', 'AM']. Splits using digits as the delimiter: ['On ', 'th Jan ', ', at ', ':', ' AM'].

Python
from re import split

print(split('\W+', 'Words, words , Words'))
print(split('\W+', "Word's words Words"))
print(split('\W+', 'On 12th Jan 2016, at 11:02 AM'))
print(split('\d+', 'On 12th Jan 2016, at 11:02 AM'))

Output
['Words', 'words', 'Words']
['Word', 's', 'words', 'Words']
['On', '12th', 'Jan', '2016', 'at', '11', '02', 'AM']
['On ', 'th Jan ', ', at ', ':', ' AM']

Example 2:

First statement splits the string at the first occurrence of one or more digits: ['On ', 'th Jan 2016, at 11:02 AM']. second splits the string using lowercase letters a to f as delimiters, case-insensitive: ['', 'y, ', 'oy oh ', 'oy, ', 'ome here']. Third splits the string using lowercase letters a to f as delimiters, case-sensitive: ['', 'ey, Boy oh ', 'oy, ', 'ome here'].

Python
import re
print(re.split('\d+', 'On 12th Jan 2016, at 11:02 AM', 1))
print(re.split('[a-f]+', 'Aey, Boy oh boy, come here', flags=re.IGNORECASE))
print(re.split('[a-f]+', 'Aey, Boy oh boy, come here'))

Output
['On ', 'th Jan 2016, at 11:02 AM']
['', 'y, ', 'oy oh ', 'oy, ', 'om', ' h', 'r', '']
['A', 'y, Boy oh ', 'oy, ', 'om', ' h', 'r', '']

4. re.sub() 

The ‘sub’ in the function stands for SubString, a certain regular expression pattern is searched in the given string(3rd parameter), and upon finding the substring pattern is replaced by repl(2nd parameter), count checks and maintains the number of times this occurs. 

Syntax:

 re.sub(pattern, repl, string, count=0, flags=0)

Example 1:

  • First statement replaces all occurrences of ‘ub’ with ‘~*’ (case-insensitive): 'S~*ject has ~*er booked already'.
  • Second statement replaces all occurrences of ‘ub’ with ‘~*’ (case-sensitive): 'S~*ject has Uber booked already'.
  • Third statement replaces the first occurrence of ‘ub’ with ‘~*’ (case-insensitive): 'S~*ject has Uber booked already'.
  • Fourth replaces ‘AND’ with ‘ & ‘ (case-insensitive): 'Baked Beans & Spam'.
Python
import re
print(re.sub('ub', '~*', 'Subject has Uber booked already', 
             flags=re.IGNORECASE))
print(re.sub('ub', '~*', 'Subject has Uber booked already'))
print(re.sub('ub', '~*', 'Subject has Uber booked already',
             count=1, flags=re.IGNORECASE))
print(re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', 
             flags=re.IGNORECASE))

Output
S~*ject has ~*er booked already
S~*ject has Uber booked already
S~*ject has Uber booked already
Baked Beans & Spam

5. re.subn() 

subn() is similar to sub() in all ways, except in its way of providing output. It returns a tuple with a count of the total of replacement and the new string rather than just the string. 

Syntax:

 re.subn(pattern, repl, string, count=0, flags=0)

Example:

re.subn() replaces all occurrences of a pattern in a string and returns a tuple with the modified string and the count of substitutions made. It’s useful for both case-sensitive and case-insensitive substitutions.

Python
import re

print(re.subn('ub', '~*', 'Subject has Uber booked already'))

t = re.subn('ub', '~*', 'Subject has Uber booked already',
            flags=re.IGNORECASE)
print(t)
print(len(t))
print(t[0])

Output
('S~*ject has Uber booked already', 1)
('S~*ject has ~*er booked already', 2)
2
S~*ject has ~*er booked already

6. re.escape()

Returns string with all non-alphanumerics backslashed, this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.

Syntax:

re.escape(string)

Example:

re.escape() is used to escape special characters in a string, making it safe to be used as a pattern in regular expressions. It ensures that any characters with special meanings in regular expressions are treated as literal characters.

Python
import re
print(re.escape("This is Awesome even 1 AM"))
print(re.escape("I Asked what is this [a-9], he said \t ^WoW"))

Output
This\ is\ Awesome\ even\ 1\ AM
I\ Asked\ what\ is\ this\ \[a\-9\]\,\ he\ said\ \    \ \^WoW

7. re.search()

This method either returns None (if the pattern doesn’t match), or a re.MatchObject contains information about the matching part of the string. This method stops after the first match, so this is best suited for testing a regular expression more than extracting data.

Example: Searching for an occurrence of the pattern

This code uses a regular expression to search for a pattern in the given string. If a match is found, it extracts and prints the matched portions of the string.

In this specific example, it searches for a pattern that consists of a month (letters) followed by a day (digits) in the input string “I was born on June 24”. If a match is found, it prints the full match, the month, and the day.

Python
import re
regex = r"([a-zA-Z]+) (\d+)"

match = re.search(regex, "I was born on June 24")
if match != None:
    print ("Match at index %s, %s" % (match.start(), match.end()))
    print ("Full match: %s" % (match.group(0)))
    print ("Month: %s" % (match.group(1)))
    print ("Day: %s" % (match.group(2)))

else:
    print ("The regex pattern does not match.")

Output
Match at index 14, 21
Full match: June 24
Month: June
Day: 24

SETS

A Set is a set of characters enclosed in ‘[]’ brackets. Sets are used to match a single character in the set of characters specified between brackets. Below is the list of Sets:

SetDescription
\{n,\}Quantifies the preceding character or group and matches at least n occurrences.
*Quantifies the preceding character or group and matches zero or more occurrences.
[0123]Matches the specified digits (0, 1, 2, or 3)
[^arn] matches for any character EXCEPT a, r, and n
\dMatches any digit (0-9).
[0-5][0-9]matches for any two-digit numbers from 00 and 59
\wMatches any alphanumeric character (a-z, A-Z, 0-9, or _).
[a-n]Matches any lower case alphabet between a and n.
\DMatches any non-digit character.
[arn]matches where one of the specified characters (a, r, or n) are present
[a-zA-Z]matches any character between a and z, lower case OR upper case
[0-9]matches any digit between 0 and 9

Match Object

A Match object contains all the information about the search and the result and if there is no match found then None will be returned. Let’s see some of the commonly used methods and attributes of the match object.

1. Getting the string and the regex

match.re attribute returns the regular expression passed and match.string attribute returns the string passed.

Example: Getting the string and the regex of the matched object

The code searches for the letter “G” at a word boundary in the string “Welcome to GeeksForGeeks” and prints the regular expression pattern (res.re) and the original string (res.string).

Python
import re
s = "Welcome to GeeksForGeeks"
res = re.search(r"\bG", s)

print(res.re)
print(res.string)

Output
re.compile('\\bG')
Welcome to GeeksForGeeks

2. Getting index of matched object

  • start() method returns the starting index of the matched substring
  • end() method returns the ending index of the matched substring
  • span() method returns a tuple containing the starting and the ending index of the matched substring

Example: Getting index of matched object 

The code searches for the substring “Gee” at a word boundary in the string “Welcome to GeeksForGeeks” and prints the start index of the match (res.start()), the end index of the match (res.end()), and the span of the match (res.span()).

Python
import re

s = "Welcome to GeeksForGeeks"

res = re.search(r"\bGee", s)

print(res.start())
print(res.end())
print(res.span())

Output
11
14
(11, 14)

3. Getting matched substring

group() method returns the part of the string for which the patterns match. See the below example for a better understanding.

Example: Getting matched substring 

The code searches for a sequence of two non-digit characters followed by a space and the letter ‘t’ in the string “Welcome to GeeksForGeeks” and prints the matched text using res.group().

Python
import re
s = "Welcome to GeeksForGeeks"
res = re.search(r"\D{2} t", s)
print(res.group())

Output
me t

In the above example, our pattern specifies for the string that contains at least 2 characters which are followed by a space, and that space is followed by a t. 

Related Article : 
https://www.geeksforgeeks.org/regular-expressions-python-set-1-search-match-find/

Reference: 
https://docs.python.org/2/library/re.html

We have discussed RegEx in Python, we have gone through the metacharacters, functions and special sequences in RegEx Python.

Regular Expression is a very important concept in Python, we have tried to explain it in a easy way. Hope it helps in your Python Journey!! 



Previous Article
Next Article

Similar Reads

How Can I Find All Matches to a Regular Expression in Python?
In Python, regular expressions (regex) are a powerful tool for finding patterns in text. Whether we're searching through logs, extracting specific data from a document, or performing complex string manipulations, Python's re module makes working with regular expressions straightforward. In this article, we will learn, how we can find all matches of
3 min read
Regular Expression to Validate a Bitcoin Address
BITCOIN is a digital currency. During the digital currency transaction, a BTC address is required to verify the legality of a bitcoin wallet a Bitcoin address is a long set of alphanumeric characters that contains numbers and letters. A Bitcoin address indicates the source or destination of a Bitcoin payment. A Bitcoin wallet is a digital wallet th
6 min read
Regex in Python to put spaces between words starting with capital letters
Given an array of characters, which is basically a sentence. However, there is no space between different words and the first letter of every word is in uppercase. You need to print this sentence after the following amendments: Put a single space between these words. Convert the uppercase letters to lowercase Examples: Input : BruceWayneIsBatmanOut
2 min read
Python Regex to extract maximum numeric value from a string
Given an alphanumeric string, extract maximum numeric value from that string. Alphabets will only be in lower case. Examples: Input : 100klh564abc365bgOutput : 564Maximum numeric value among 100, 564 and 365 is 564.Input : abchsd0sdhsOutput : 0Python Regex to extract maximum numeric value from a stringThis problem has existing solution please refer
2 min read
Find all the patterns of “1(0+)1” in a given string using Python Regex
A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0’s. Count all such patterns. The patterns are allowed to overlap. Note : It contains digits and lowercase characters only. The string is not necessarily a binary. 100201 is not a valid pattern. Examples: Input : 1101001 Output : 2 Input : 1000
2 min read
The most occurring number in a string using Regex in python
Given a string str, the task is to extract all the numbers from a string and find out the most occurring element of them using Regex Python. It is guaranteed that no two element have the same frequency Examples: Input :geek55of55geeks4abc3dr2 Output :55Input :abcd1def2high2bnasvd3vjhd44Output :2Approach:Extract all the numbers from a string str usi
2 min read
Python | Parse a website with regex and urllib
Let's discuss the concept of parsing using python. In python we have lot of modules but for parsing we only need urllib and re i.e regular expression. By using both of these libraries we can fetch the data on web pages. Note that parsing of websites means that fetch the whole source code and that we want to search using a given url link, it will gi
2 min read
Name validation using IGNORECASE in Python Regex
In this article, we will learn about how to use Python Regex to validate name using IGNORECASE. re.IGNORECASE : This flag allows for case-insensitive matching of the Regular Expression with the given string i.e. expressions like [A-Z] will match lowercase letters, too. Generally, It's passed as an optional argument to re.compile(). Let's consider a
2 min read
Verbose in Python Regex
In this article, we will learn about VERBOSE flag of the re package and how to use it. re.VERBOSE : This flag allows you to write regular expressions that look nicer and are more readable by allowing you to visually separate logical sections of the pattern and add comments. Whitespace within the pattern is ignored, except when in a character class,
3 min read
Python | Swap Name and Date using Group Capturing in Regex
In this article, we will learn how to swap Name and Date for each item in a list using Group Capturing and Numeric Back-referencing feature in Regex . Capturing Group : Parentheses groups the regex between them and captures the text matched by the regex inside them into a numbered group i.e ([\w ]+) which can be reused with a numbered back-referenc
3 min read
Python Regex: re.search() VS re.findall()
Prerequisite: Regular Expression with Examples | Python A Regular expression (sometimes called a Rational expression) is a sequence of characters that define a search pattern, mainly for use in pattern matching with strings, or string matching, i.e. "find and replace"-like operations. Regular expressions are a generalized way to match patterns with
3 min read
Categorize Password as Strong or Weak using Regex in Python
Given a password, we have to categorize it as a strong or weak one. There are some checks that need to be met to be a strong password. For a weak password, we need to return the reason for it to be weak. Conditions to be fulfilled are: Minimum 9 characters and maximum 20 characters.Cannot be a newline or a spaceThere should not be three or more rep
2 min read
How to check if a string starts with a substring using regex in Python?
Prerequisite: Regular Expression in Python Given a string str, the task is to check if a string starts with a given substring or not using regular expression in Python. Examples: Input: String: "geeks for geeks makes learning fun" Substring: "geeks" Output: True Input: String: "geeks for geeks makes learning fun" Substring: "makes" Output: FalseChe
3 min read
Python Regex - re.MatchObject.start() and re.MatchObject.end() functions
In this article, we are going to see re.MatchObject.start() and re.MatchObject.end() regex methods. re.MatchObject.start() This method returns the first index of the substring matched by group. Syntax: re.MatchObject.start([group]) Parameter: group: (optional) group defaults to zero (meaning the whole matched substring). Return -1 if group exists b
2 min read
re.MatchObject.span() Method in Python - regex
re.MatchObject.span() method returns a tuple containing starting and ending index of the matched string. If group did not contribute to the match it returns(-1,-1). Syntax: re.MatchObject.span() Parameters: group (optional) By default this is 0. Return: A tuple containing starting and ending index of the matched string. If group did not contribute
2 min read
re.MatchObject.group() function in Python Regex
re.MatchObject.group() method returns the complete matched subgroup by default or a tuple of matched subgroups depending on the number of arguments Syntax: re.MatchObject.group([group]) Parameter: group: (optional) group defaults to zero (meaning that it it will return the complete matched string). Return -1 if group exists but did not contribute t
3 min read
re.MatchObject.groupdict() function in Python - Regex
This method returns a dictionary with the groupname as keys and the matched string as the value for that key. Syntax: re.MatchObject.groupdict() Return: A dictionary with groupnames as the keys and matched string as the value for the key. AttributeError: If a matching pattern is not found then it raise AttributeError. Consider the below example: Ex
3 min read
re.MatchObject.groups() function in Python - Regex
This method returns a tuple of all matched subgroups. Syntax: re.MatchObject.groups() Return: A tuple of all matched subgroups AttributeError: If a matching pattern is not found then it raise AttributeError. Consider the below example: Example 1: C/C++ Code import re """We create a re.MatchObject and store it in match_object variable
2 min read
Parsing and Processing URL using Python - Regex
Prerequisite: Regular Expression in Python URL or Uniform Resource Locator consists of many information parts, such as the domain name, path, port number etc. Any URL can be processed and parsed using Regular Expression. So for using Regular Expression we have to use re library in Python. Example: URL: https://www.geeksforgeeks.org/courses When we
3 min read
Python RegEx
In this tutorial, you'll learn about RegEx and understand various regular expressions. Regular ExpressionsWhy Regular ExpressionsBasic Regular ExpressionsMore Regular ExpressionsCompiled Regular Expressions A RegEx is a powerful tool for matching text, based on a pre-defined pattern. It can detect the presence or absence of a text by matching it wi
9 min read
Converting a 10 digit phone number to US format using Regex in Python
Text preprocessing is one of the most important tasks in Natural Language Processing. You may want to extract number from a string. Writing a manual script for such processing task requires a lot of effort and at most times it is prone to errors. Keeping in view the importance of these preprocessing tasks, the concept of Regular Expression have bee
1 min read
Python - Regex Lookahead
Lookahead is used as an assertion in Python regular expressions to determine success or failure whether the pattern is ahead i.e to the right of the parser's current position. They don't match anything. Hence, they are called as zero-width assertions. Syntax: # Positive lookahead (?=<lookahead_regex>) Example 1: C/C++ Code # importing regex i
2 min read
How to check a valid regex string using Python?
A Regex (Regular Expression) is a sequence of characters used for defining a pattern. This pattern could be used for searching, replacing and other operations. Regex is extensively utilized in applications that require input validation, Password validation, Pattern Recognition, search and replace utilities (found in word processors) etc. This is du
6 min read
Python - Regex Lookbehind
Regex Lookbehind is used as an assertion in Python regular expressions(re) to determine success or failure whether the pattern is behind i.e to the right of the parser's current position. They don't match anything. Hence, Regex Lookbehind and lookahead are termed as a zero-width assertion. Syntax: # Positive lookbehind (?<=<lookbehind_regex
2 min read
Python - Check if String Contain Only Defined Characters using Regex
In this article, we are going to see how to check whether the given string contains only a certain set of characters in Python. These defined characters will be represented using sets. Examples: Input: ‘657’ let us say regular expression contains the following characters- (‘78653’) Output: Valid Explanation: The Input string only consists of charac
2 min read
Regex Cheat Sheet - Python
Regex or Regular Expressions are an important part of Python Programming or any other Programming Language. It is used for searching and even replacing the specified text pattern. In the regular expression, a set of characters together form the search pattern. It is also known as the reg-ex pattern. The tough thing about Regex is not learning or un
9 min read
Pattern matching in Python with Regex
You may be familiar with searching for text by pressing ctrl-F and typing in the words you’re looking for. Regular expressions go one step further: They allow you to specify a pattern of text to search for. In this article, we will see how pattern matching in Python works with Regex. Regex in PythonRegular expressions, also called regex, are descri
8 min read
How to Import Regex in Python
Regex is a built-in library in Python. You can use the re module to work with regular expressions. Here are some basic examples of how to use the re module in Python: Examples of Regular Expression in PythonWe can import it in our Python by writing the following import statement in the Python script. import re//orimport re as regexNow let us see a
2 min read
Python Regex: Replace Captured Groups
Regular Expressions, often abbreviated as Regex, are sequences of characters that form search patterns. They are powerful tools used in programming and text processing to search, match, and manipulate strings. Think of them as advanced search filters that allow us to find specific patterns within a text, such as email addresses, dates, phone number
5 min read
How to Remove repetitive characters from words of the given Pandas DataFrame using Regex?
Prerequisite: Regular Expression in Python In this article, we will see how to remove continuously repeating characters from the words of the given column of the given Pandas Dataframe using Regex. Here, we are actually looking for continuously occurring repetitively coming characters for that we have created a pattern that contains this regular ex
2 min read
Article Tags :
Practice Tags :