Open Source Overview of Regular Expressions and Regex in Python

Overview of Regular Expressions and Regex in Python

Regular expressions – or Regex – is a programming term, used across many programming languages, that describes patterns used to match characters and combinations of characters, in a string. Languages that use Regex for pattern matching include Perl, Python, Java, and most modern languages.

The purpose of using regular expressions is to identify patterns that a developer can then act upon. Some common examples might include finding and replacing unwanted characters in a string, querying a string or group of strings to find specific information, or to validate input.

Uses of Regular Expressions and Regex

For instance, a programmer might want to find and replace certain character cases in a string. Suppose you have a string that contains the sentence: “HowAreYouDoingToday”. This is known as camel case and is a mixture of uppercase and lowercase characters in a string. Ideally, you would want this string to read “How are you today” instead. Using Regex, you could search and replace the characters in the string with the proper case and also add a space between each word.

Another example use case for regular expressions that is very popular is checking password patterns. Often a website or software program will require a user to create a password. In order to keep accounts safe, these websites and apps require strong passwords that typically contain a combination of uppercase and lowercase letters, numbers, and special characters. You can use a Regex to check the password a user inputs into a form to make sure they contain the appropriate mix of characters and to ensure the password is the proper length.

Most programming languages that support Regex, regular expressions, and pattern searching have modules or libraries that support them.

Regular Expressions in Python

Here is an example of a Regex written using Python 3. We import the re module first so that we can use it to search within a string. Here is how that looks in Python code :

Note: The # – or hashtag – is used for notation and leaving comments to explain the code.

Import re # Used to import the RegEx module

txt = "My name is James Payne" # Creating a string with characters in it

x ="^My.*Payne$", txt) # Searching for any text in a string that contains “Payne”

# If “Payne” is found, it will print the first part; if not, it will print the second part

if x:
  print("Amazing...we have found Payne!")
  print("We did not find the person you were looking for...")

In this instance, since the text “Payne” is found, the output of our program would be:

Amazing...we have found Payne!

In this example code, we use one of the many functions and methods that reside within the re module, namely, search() or As you can imagine search() is used to search a string for a given value to see if it exists.

Before we can use search(), we actually need to import it. There are two simple ways we can import the search() function. First, we can import the entire re module, as we did in our example above:

Import re #importing the entire re module

Or, we could just import the parts of the re module that we want to use – in this instance, search():

from re import search

Either method is perfectly acceptable.

Regex Functions in Python

Python’s re module has four functions you can use to search a string for a matching criteria. They are as follows:

Python Regex Functions:

  • search: used to return a match object if a match is found anywhere inside of a string.
  • split: used to return a list where a string has been split at each match.
  • sub: used to replace one or more matches with a given string.
  • findall: used to return a list consisting of all found matches.

We will delve into Regex and regular expressions in Python much more in-depth in a future article. Stay tuned!

Latest Posts

Related Stories