Similar to Java. Use re.error exception:

import re

try:
    re.compile('[')
    is_valid = True
except re.error:
    is_valid = False

exception re.error

Exception raised when a string passed to one of the functions here is not a valid regular expression (for example, it might contain unmatched parentheses) or when some other error occurs during compilation or matching. It is never an error if a string contains no match for a pattern.

Answer from falsetru on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-check-a-valid-regex-string-using-python
How to check a valid regex string using Python? - GeeksforGeeks
July 31, 2023 - The pattern provided is invalid as it contains an unclosed character class (in regex square brackets `[ ]`are used for defining a character class). We placed the re.compile() (used to compile regex patterns) function within the try block. This will firstly try to compile the pattern and if any exception occurs during the compilation, it would firstly check whether it is re.error, if it is then only the except block will execute.
Discussions

How to check is a string is a valid regex - python? - Stack Overflow
If so, what is it? ... @Haidro no, think of missing brackets and illegal use of special characters – Robin Krahl Oct 28 '13 at 9:23 · @RobinKrahl Ah, touché ;) – TerryA Oct 28 '13 at 9:24 ... is there a way i can use try...except and re.compile in python? will the re.compile validate regex? More on stackoverflow.com
🌐 stackoverflow.com
December 23, 2019
Python: check regex expression - Stack Overflow
I generate my own regex expression and I would like to check if it is valid. For example if I have regex "[a-zA-Z0-9]" it will be valid but "[a-zA-Z0s-9]" not. More on stackoverflow.com
🌐 stackoverflow.com
python - Verify if string is valid regex? - Stack Overflow
EDIT: I cannot use python's 're' This is for an assignment so I don't want the answer in code, rather just some tips in the right direction. I am trying to code a function that returns True if s... More on stackoverflow.com
🌐 stackoverflow.com
March 11, 2014
python - Validate user input using regular expressions - Stack Overflow
I would like to be able to validate that the user has entered a valid name within my program. If they have not entered a valid name i want the program to continue to prompt them to enter their name More on stackoverflow.com
🌐 stackoverflow.com
🌐
Regex101
regex101.com
regex101: build, test, and debug regex
Regular expression tester with syntax highlighting, explanation, cheat sheet for PHP/PCRE, Python, GO, JavaScript, Java, C#/.NET, Rust.
🌐
Python documentation
docs.python.org › 3 › library › re.html
re — Regular expression operations — Python 3.14.3 ...
1 week ago - The third-party regex module, which has an API compatible with the standard library re module, but offers additional functionality and a more thorough Unicode support. A regular expression (or RE) specifies a set of strings that matches it; the functions in this module let you check if a particular string matches a given regular expression (or if a given regular expression matches a particular string, which comes down to the same thing).
🌐
QA Touch
qatouch.com › home › python regex testing: a comprehensive guide with examples
Python Regex Testing: A Comprehensive Guide with Examples
June 24, 2025 - If you want to extract URLs from a text, use re.findall(): Imagine you’re working on a form where users enter their phone numbers. You want to validate that the phone numbers are in a valid format and extract them for further processing. Here’s how you can achieve this using Python regex: In this example, the phone_pattern regex is ...
Find elsewhere
🌐
W3Schools
w3schools.com › python › python_regex.asp
Python RegEx
Note: If there is no match, the value None will be returned, instead of the Match Object. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com · If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com · HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
🌐
DNMTechs
dnmtechs.com › checking-if-a-string-is-a-valid-regex-in-python
Checking if a String is a Valid Regex in Python – DNMTechs – Sharing and Storing Technology Knowledge
The most straightforward way to check if a string is a valid regex in Python is by attempting to compile it using the re.compile() function from the re module.
🌐
Ruan Bekker's Blog
ruan.dev › blog › 2019 › 02 › 27 › how-to-validate-strings-in-python-with-regex
How to Validate Strings in Python with Regex | Ruan Bekker's Blog
February 27, 2019 - >>> import re >>> def validation_check(input_string): ... regex = re.compile('my-random-[a-z]{3}-string_[0-9a-z]{32}\Z', re.I) ... match = regex.match(str(input_string)) ... return bool(match) ... >>> mystring = generate_string() >>> mystring 'my-random-ngt-string_6346145281738193742120539836241' >>> validate = validation_check(mystring) >>> if validate == True: ... print('The string {} is valid'.format(mystring)) ...
🌐
Pythex
pythex.org
Pythex: a Python regular expression editor
Pythex is a real-time regular expression editor for Python, a quick way to test your regular expressions.
🌐
Delft Stack
delftstack.com › home › howto › python › python check if string matches regex
How to Check if String Matches Regex in Python | Delft Stack
March 4, 2025 - The re.fullmatch() function checks if the entire string “example@example.com” fits this pattern. Since it does, the function returns a match object, confirming that the email is valid.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › introduction to regular expression in python | regex in python
Introduction to Regular Expression in Python | Regex in Python
November 18, 2024 - This method checks for a match only at the beginning of the string. So, if a match is found in the first line, it returns the match object. But if a match is found in some other line, it returns null. Here is an example: import re String ='''learning regex with great learning is easy also regex is very useful for string matching.
🌐
Reddit
reddit.com › r/learnpython › use regex to check if string contains only allowed characters
r/learnpython on Reddit: Use regex to check if string contains only allowed characters
November 24, 2021 -

I want to check if a string contains only letters a, b, c, and +.

I am checking in this way:

re.compile(r'[abc\+]')

But when I check against

acbbbaa+aa and aabbi+aaa I got both matched.

What am I doing wrong?

🌐
ACCELQ
accelq.com › home › master python regex testing: your go-to guide for test automation
Your Go-To Guide to Python Regex Testing for Automation
November 6, 2025 - Python Regex in automation testing with examples helps you validate, search, and manipulate text efficiently with regular expressions.
🌐
Qodex
qodex.ai › home › all tools › getting started › date regex python validator
Date Regex Python Validator — Test Patterns Online
Regex ensures the structure is correct, but not the logical validity (e.g. 31st Feb is still “valid” structurally). Regular expressions are an essential tool for data validation, especially when you need to quickly check whether an input fits a specific date format like YYYY-MM-DD, MM/DD/YYYY, or DD-MM-YYYY. Their power lies in their flexibility and speed—regex can instantly scan and confirm if the general pattern of a date is present, catching typos or format errors before they cause problems downstream.
Rating: 4.9 ​ - ​ 60 votes