# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
([0-9]+)(?:-([0-9]+)|\s*over)
"""
test_str = ("'m trying to match speed descriptions of highway tickets, for example,text lines:\n\n"
"\"L A 16-25MPH\" should return 2 groups: 16, 25 \n"
"\"LIMITED ACCESS SPEED I-75\" should return no matches.\n"
"\"LMT ACC 6-10\" should return 2 groups: 6, 10 \n"
"\"6 OVER\" should return 1 group: 6\n\n"
"I'm OK with all of the above situations, but I run into issues for strings with numbers that aren't related to speed, for example:\n\n")
subst = ""
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE | re.IGNORECASE | re.VERBOSE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html