Importing Regex and manipulating strings
Server Academy Members Only
Sorry, this lesson is only available to Server Academy members. Create a free account now to get instant access to this and more free courses. Click the Sign Up Free button below to get access to our free courses now, or sign in if you have an account.
Welcome to the tutorial on "Employing Regex and Shaping Strings"! We're about to dive into the world of regular expressions (Regex) in Python, taking a journey through the re module and its many utilities. The re module is a toolbox for everything from pattern matching to string division, and all it takes to unlock these tools is a simple import statement. Let's get started!
Kick off by drawing the re module into your Python script:
import re
Having brought in the re module, a myriad of Regex functions is now at your fingertips. Let's inspect some of the key functions:
re.match(pattern, string)
: Examines whether the pattern is present at the outset of the string. It offers a match object for a successful match, or None otherwise. For instance:
import re
pattern = r"Hello"
string = "Hello, World!"
match = re.match(pattern, string)
if match:
print("Pattern matched!")
else:
print("No match found.")
In the above snippet, r"Hello" is pitted against "Hello, World!". A successful match at the string's start triggers the "Pattern matched!" message.
re.search(pattern, string)
: Hunts for the pattern's initial appearance within the string, returning a match object for a hit or None otherwise. As an example:
match = re.search(pattern, string
)
Here, r"World" is sought in "Hello, World!". The pattern's presence prompts the "Pattern found!" message.
re.findall(pattern, string)
: Retrieves all instances of the pattern within the string, returning them in a list. For example:
import re
pattern = r"\d+"
string = "I have 3 apples and 5 oranges."
matches = re.findall(pattern, string)
print(matches)
The snippet uses r"\d+" to detect one or more digit characters in "I have 3 apples and 5 oranges.". With re.findall(), the result is ['3', '5'] - the discovered matches.
re.sub(pattern, repl, string)
: Swaps all instances of the pattern in the string with a chosen replacement. For instance:
import re
pattern = r"apple"
string = "I love apple pie."
replaced_string = re.sub(pattern, "banana", string)
print(replaced_string)
The script replaces r"apple" with "banana" in "I love apple pie.". The outcome via re.sub() is the adjusted string "I love banana pie.".
re.split(pattern, string)
: Dismantles the string at the pattern's occurrences, returning the resultant substrings in a list. As an example:
import re
pattern = r"\s+"
string = "Hello World!"
split_list = re.split(pattern, string)
print(split_list)
The command r"\s+" breaks down "Hello World!" wherever one or more whitespace characters occur. The result, ['Hello', 'World!'], is the list of substrings after the split.
These are just a taste of the re module's offerings for Regex tasks in Python. With just an import statement, you unlock a plethora of operations like pattern matching, searching, replacement, and string splitting. Try various patterns and delve into the documentation for advanced features.
In subsequent tutorials, we'll delve deeper into Regex, tackling more complex problems and honing your string shaping abilities. Keep practicing and harness the might of Regex in your Python endeavors!
Server Academy Members Only
Want to access this lesson? Just sign up for a free Server Academy account and you'll be on your way. Already have an account? Click the Sign Up Free button to get started..
Saving Progress...
Python 3 for Beginners
Installing Python on Windows • 1hr 17min
0 / 4 lessons complete
Section Overview
Free Preview Lesson
Text | 1 min
Downloading and Installing Python on Windows
Free Preview Lesson
Text | 8 min
Installing and configuring VS Code for Python
Free Preview Lesson
Text | 8 min
Lab: Installing Python
Devops Lab | 60
Python Basics • 28min
0 / 7 lessons complete
Section Overview
Text | 2 min
Executing Python Code
Free Preview Lesson
Text | 3 min
Python 3 Syntax
Free Preview Lesson
Text | 5 min
Help! Python Keywords
Free Preview Lesson
Text | 4 min
Printing to the console!
Text | 5 min
Python Operators
Text | 4 min
Section Review
Quiz | 5 min
Python Variables • 41min
0 / 8 lessons complete
Section Overview
Text | 4 min
Casting
Text | 4 min
Strings
Text | 5 min
Booleans
Text | 8 min
User Input
Text | 2 min
Numbers
Text | 7 min
NoneType
Free Preview Lesson
Text | 5 min
Assignment: Write a Mad Libs Script
Text | 6 min
Even more Python Variables! • 41min
0 / 6 lessons complete
Python Lists
Text | 8 min
Python Tuples
Text | 7 min
Python Sets
Text | 7 min
Frozensets
Text | 6 min
Dictionaries
Text | 8 min
Iterator and Iterable
Text | 5 min
Conditional Statements • 15min
0 / 3 lessons complete
Writing Functions • 30min
0 / 5 lessons complete
Section Overview
Text | 3 min
Defining Functions
Text | 4 min
Importing functions from another file
Text | 4 min
Assignment: Rock, paper, scissors
Text | 12 min
Assignment: Count Dictionary Words
Text | 7 min
Python Loops • 23min
0 / 5 lessons complete
Section Overview
Text | 2 min
For In Loops
Text | 5 min
While Loops
Text | 5 min
Nested Loops
Text | 3 min
Python Loops Challenge!
Text | 8 min
Python PIP and Modules • 18min
0 / 4 lessons complete
Section Overview
Text | 3 min
Installing Python PIP
Text | 4 min
Installing Modules with PIP
Text | 5 min
Importing Modules
Text | 6 min
RegEx • 26min
0 / 4 lessons complete
Working with APIs • 12min
0 / 3 lessons complete
Making HTTP Requests
Text | 3 min
Working with JSON
Text | 5 min
Get your weather with the OpenWeatherMap
Text | 4 min
Course Conclusion • 2min
0 / 1 lessons complete