DEV Community

Dendi Handian
Dendi Handian

Posted on • Updated on

Regular Expression (Regex) with Python

The Playground

the re package is a built-in package in python to work with Regular Expression. You can use the package within any python environment like (Anaconda) Jupyter Notebook, Google Colab, even from your favorite editor like VS Code. I will use the example text from the You can also test and play with expression there before applying it to re functions. So here is the starter in our python/ipynb file:

text = "\
RegExr was created by, and is proudly hosted by Media Temple. \
Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expression with Tests mode. \
The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns. \
Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English. \
Enter fullscreen mode Exit fullscreen mode

search function

The search function is used to find out whether the expression match any of the string and return Match object when it's matched.

Here is the example of how to search if there is any word that ends with .com:

from re import search

match = search('([a-zA-Z]*.com)', text)

if match:
  print("not matched")

print("span:", match.span())
print("slice text with match span:", text[match.span()[0]:match.span()[1]])
Enter fullscreen mode Exit fullscreen mode

findall function

The findall function used to get a list of the matched. Here is an example of finding all word that has y character in it:

from re import findall

matched_list = findall('([a-zA-Z]*y[a-zA-Z]*)', text)

print("list of word:", matched_list)
print("words count:", len(matched_list))
Enter fullscreen mode Exit fullscreen mode

split function

The split function used to get a list of string that splitted by the given expression or pattern. Here is the example of getting all words that splitted by whitespace character:

from re import split

splitted_words = split('\s', text)

print("list of word:", splitted_words)
print("words count:", len(splitted_words))
Enter fullscreen mode Exit fullscreen mode

sub function

The sub function used to replace certain string based on the expression from the original text. Here is the example of changing the original text by replacing & with and:

from re import sub

new_text = sub('&', 'and', text)

Enter fullscreen mode Exit fullscreen mode

Top comments (0)