You need to escape your backslash:
p.sub('gray \\1', s)
alternatively you can use a raw string as you already did for the regex:
p.sub(r'gray \1', s)
More Related Contents:
- Speed up millions of regex replacements in Python 3
- Python string.replace regular expression [duplicate]
- How to input a regex in string.replace?
- How to replace the first occurrence of a regular expression in Python?
- Mass string replace in python?
- Regexp finding longest common prefix of two strings
- Remove non-ASCII characters from a string using python / django
- feature extraction in python for nlp
- Best way to replace multiple characters in a string?
- Replace all elements of Python NumPy Array that are greater than some value
- Replacing few values in a pandas dataframe column with another value
- Reversing a regular expression in Python
- re.findall not returning full match?
- re.sub erroring with “Expected string or bytes-like object”
- Can’t use ‘\1’ backreference to capture-group in a function call in re.sub() repr expression
- Replace invalid values with None in Pandas DataFrame
- Python regex to match dates
- python: why does replace not work?
- regexes: How to access multiple matches of a group? [duplicate]
- Grep and Python
- Regexp to remove specific number of occurrences of character only
- Regex and unicode
- How to parse complex text files using Python?
- How to process huge text files that contain EOF / Ctrl-Z characters using Python on Windows?
- How can I find all placeholders for str.format in a python string using a regex? [duplicate]
- How to add multiple arguments to my custom template filter in a django template?
- restrict 1 word as case sensitive and other as case insensitive in python regex | (pipe)
- Python TypeError on regex [duplicate]
- In regex, what does [\w*] mean?
- Adding characters after a certain word in Python using regex