Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

RegEx

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 has a built-in package called re, which can be used to work with Regular Expressions.
Import the re module.

# Search the string to see if it starts with "The" and ends with "India":
import re

txt = "The rain in India"
x = bool(re.search("^The.*India$", txt))
print(x)
# prints True

RegEx Functions

The re module offers a set of functions that allows us to search a string for a match.

findall() function

The findall() function returns a list containing all matches.

Note: If no matches are found, an empty list is returned.

import re

txt = "The rain in India"
x = re.findall("in", txt)
print(x)
# prints ['in', 'in']

search() function

The search() function searches the string for a match, and returns a Match object if there is a match.

If there is more than one match, only the first occurrence of the match will be returned.

Note: If no matches are found, the value None is returned.

# Search for the first white-space character in the string
import re

txt = "The rain in India"
x = re.search("\s", txt)

print("The first white-space character is located in position:", x.start())
# prints The first white-space character is located in position: 3

split() function

The split() function returns a list where the string has been split at each match.

# Split at each white-space character:
import re

#Split the string at every white-space character:

txt = "The rain in India"
x = re.split("\s", txt)
print(x)
# prints ['The', 'rain', 'in', 'India']

You can control the number of occurrences by specifying the maxsplit parameter.

# Split the string only at the first occurrence
import re

txt = "The rain in India"
x = re.split("\s", txt, 1)
print(x)
# prints ['The', 'rain in India']

sub() function

The sub() function replaces the matches with the text of your choice.

# Replace every white-space character with the number 9:
import re

txt = "The rain in India"
x = re.sub("\s", "9", txt)
print(x)
# prints The9rain9in9India

You can control the number of replacements by specifying the count parameter.

import re

txt = "The rain in India"
x = re.sub("\s", "9", txt, 2)
print(x)
# prints The9rain9in India

You can learn more about RegEx from here

Try the following example in the editor below.

Given a string txt, perform the operations as defined in the comments.

Start solving RegEx on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test