regexmatch.dev
Explore
Open main menu
Save & Share
Facebook
Twitter
Tumblr
E-Mail
Pinterest
LinkedIn
Reddit
python regex
Published on Tue Mar 01 2022
Regex
Test String
color: "red" color: 'red' color: red # hoping to do this: match all 3 lines above and be able to replace any instance of the word red with my own custom string
Additional matching regexes for
python regex
Email
Python
Python-Dunder-Underscore
# Python Dunder Underscore Regular Expression # ## Match ## ```python # coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r'^(__version__)(\s?=\s?)+("(.*)")' test_str = "__version__ = \"0.1.1.1\"" matches = re.search(regex, test_str) if matches: print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group())) for groupNum in range(0, len(matches.groups())): groupNum = groupNum + 1 print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.group(groupNum))) # Note: for Python 2.7 compatibility, use ur'' to prefix the regex and u"" to prefix the test string and substitution. ``` ## Substitution ## ### Full String Replacement ### ```python # coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r'^(__version__)(\s?=\s?)+("(.*)")' test_str = "__version__ = \"0.1.1.1\"" subst = "[Replacement]" # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, subst, test_str, 1) 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. ``` ### Dunder Replacement ### ```python # coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r'(__version__)(\s?=\s?)+("(.*)")' test_str = "__version__ = \"0.1.1.1\"" subst = "[Dunder-Replacement] \\g<3>" # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, subst, test_str, 1) 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. ``` ### Version Replacement ### ```python # coding=utf8 # the above tag defines encoding for this document and is for Python 2.x compatibility import re regex = r'(__version__)(\s?=\s?)+("(.*)")' test_str = "__version__ = \"0.1.1.1\"" subst = " \\g<1> = [Dunder-Replacement]" # You can manually specify the number of replacements by changing the 4th argument result = re.sub(regex, subst, test_str, 1) 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. ```
Extract parts of a URL on Python
Extract parts of a URL on Python
streets with one or more names with unicode characters in python
streets with one or more names with unicode characters in python
Complete imgur link regex
This regex grabs all kinds of imgur links and groups them in meaningful names aswell. This way you have full flow-control in your python-scripts.
printf conversion specification parsing
Parses `printf` conversion specifications. This version detects multiple flags and ` `(space) flag. MSVC length modifier `I64` is also supported. I'm using this regex in my Python project, so please change accordingly if you use other flavor of regex.
Indian Phone Numbers!
This is not supported in Javascript! Works well for php, python. Examples: +91-8800119719, 08800119719, 8800119719, +918800119719 etc.
Triple Quoted String
More powerful than python style, this supports things like `"""" """"`, which matches totally. Also supports `"""a""""""b"""`, where there are two matches: `"""a"""`,`"""b"""`.
Distinguish torrent files (series vs movies)
A neat regex for finding out whether a given torrent name is a series or a movie. Returns the full name of the series with the separator needed to make it pretty (ie, replace it with space or what you want). Also returns the season number or the year for the movie/series, depending on what was previously matches. If I had done this in perl there would be much less logic needed in the regex, but python is what I'm working with so..
Color RegEx
Color RegEx