Skip to content

Instantly share code, notes, and snippets.

@alpiepho
Created October 10, 2019 12:14
Show Gist options
  • Save alpiepho/144cc3e2a938cf71164a42d4a86488b0 to your computer and use it in GitHub Desktop.
Save alpiepho/144cc3e2a938cf71164a42d4a86488b0 to your computer and use it in GitHub Desktop.
Find where regex fails to match a string using pypi package re-assert
# DESCRIPTION
# This script can be run on repl.it to find out where a regex expression
# fails. This uses the python library re-assert that can be found here:
# https://pypi.org/project/re-assert/
# SETUP
# Use online python IDE at
# https://repl.it/languages/python3
# 1) create new file 'requiements.txt' with following content:
# (remove the leading #)
#re-assert==1.0.0
# 2) replace the main.py file with the following and run
# (or cut/paste this entire file
import re
from re_assert import Matches
test = Matches('\d{3}-\d{3}-\d{4}')
test.assert_matches('1123-456-7890')
# EXAMPLE 1
# cut and paste to console
test = Matches('\d{3}-\d{3}-\d{4}') # regex here
test.assert_matches('1123-456-7890') # expect to match
# EXAMPLE 2
# cut and paste to console
test = Matches('\d{3}-\d{3}--\d{4}') # regex here
test.assert_matches('123-456-7890') # expect to match
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment