Pythonium

Python, What else ?

Pyrexp

Python Regex Tester

Your regular expression to test

Your test string



Python regex checker

Welcome to Pyrexp, our Python regex testing tool! Pyrexp strive to have a user-friendly interface to make testing your regular expressions more enjoyable. We know that regex can sometimes be hair-pullingly frustrating; we've been there ourselves!

With Pyrexp, you can ensure that your regex patterns have valid syntax, eliminating common syntax errors that often plague regex (Personally, I get lost in those parentheses… It reminds me of LISP…). Simply input your regular expression, and our tester will validate its syntax.

But that's not all – it allows you to test your regular expressions against strings, so you can see exactly how they match and identify any potential issues.

Pyrexp is also a Regex visualizer, it provides graphical visualization of your regex patterns. This visual representation offers a clear understanding of how your pattern works, making it easier to debug and optimize for your specific needs. Again, those parentheses can be confusing…

And if you need to share your regex pattern or keep it for future reference, our tool allows you to export it as a JPG image. This functionality can be handy for sharing your regex on forums (Yes, ChatGPT hasn't completely replaced them).

Mini tutorial on Regex in Python

Regular expressions (regex) are a powerful way to search, filter, or modify strings. Python provides a built-in module called re to work with regex. The re library is easy to use, and we will go over its basics together.

Importing the re module

import re

Searching for a pattern with re.search()

The re.search() function finds the first match of a pattern in a string.

import re

text = "Hello, my phone number is 07 45 89 23 67."
pattern = r"\d{2} \d{2} \d{2} \d{2} \d{2}"  # French phone number

result = re.search(pattern, text)

if result:
    print("Found number:", result.group())  # Output: "07 45 89 23 67"
else:
    print("No number found.")

Using re.findall() to find all matches

The re.findall() function finds all matches of a pattern in a string and returns them as a list.

import re

text = "Here are two numbers: 07 45 89 23 67 and 06 12 34 56 78."
pattern = r"\d{2} \d{2} \d{2} \d{2} \d{2}"

numbers = re.findall(pattern, text)

print("Found numbers:", numbers)  # Output: ['07 45 89 23 67', '06 12 34 56 78']

Replacing text with re.sub()

The re.sub() function replaces occurrences of a pattern with a new string.

import re

text = "Hello, here is my number: 07 45 89 23 67."
pattern = r"\d{2} \d{2} \d{2} \d{2} \d{2}"
new_text = re.sub(pattern, "[NUMBER MASKED]", text)

print(new_text)  # Output: "Hello, here is my number: [NUMBER MASKED]."

Capturing groups with parentheses ( )

Groups in regular expressions allow you to capture and extract specific parts of a match.

import re

text = "My email is contact@pythonium.net."
pattern = r"(\w+)@(\w+\.\w+)"  # Group for the username and domain

result = re.search(pattern, text)

if result:
    print("Username:", result.group(1))  # contact
    print("Domain:", result.group(2))  # pythonium.net

Splitting a string with re.split()

The re.split() function splits a string into a list at each match of a pattern.

import re

text = "one-two-three-four"
pattern = r"-"
words = re.split(pattern, text)

print(words)  # Output: ['one', 'two', 'three', 'four']

Why test Python regex?

As a joke, we sometimes say that testing is doubting… Unfortunately, with regex, we don't really have a choice; we have to test them to make sure we don't mess up!

You may want to ensure your regular expressions function correctly in Python, verifying their syntax and confirming their matches against strings. Additionally, testing allows you to visualize and export your regex, aiding in understanding and sharing your patterns effectively.

Pyrexp uses Regulex (Unfortunately, it doesn't seem to be maintained anymore), for the visualization of regular expressions.