In Python3, the following code works to replace a string (two or more) of *
's with x
's.
import re
re.sub(r'\*(?=\*)|(?<=\*)\*', 'x', 'Replace this *** but not this *')
# 'Replace this xxx but not this *'
But what if I also want to exempt a string of *
's that are part of a "word" like the following? (I.e. the string is attached to one or more [a-zA-Z]
characters.)
text = "Don't replace foo*** or **bar, either."
# unmodified text expected
How do I do this? I can probably match the exempted cases as well and use a replacement function to deal with them, but is there a better way?
To replace or substitute all occurrences of one character with another character, you can use the substitute function. The SUBSTITUTE function is full automatic. All you need to do is supply "old text" and "new text". SUBSTITUTE will replace every instance of the old text with the new text.
The Python replace() method is used to find and replace characters in a string. It requires a substring to be passed as an argument; the function finds and replaces it. The replace() method is commonly used in data cleaning.
Use the replace() method to replace multiple characters in a string, e.g. str. replace(/[. _-]/g, ' ') . The first parameter the method takes is a regular expression that can match multiple characters.
Method 3: Replace multiple characters using re.subn() is similar to sub() in all ways, except in its way of providing output. It returns a tuple with a count of the total of replacement and the new string rather than just the string.
regex = r"\s\*{2,}[\s\n]"
This matches 2 or more *
chars, surrounded by whitespace (or ended with a newline)
Call it maybe like this?
regex = r"\s\*{2,}[\s\n]"
def replacer(match):
return 'x' * len(match.group())
re.sub(regex, replacer, your_string_here)
This answer is inspired Danielle M.'s. This pattern below seems to give me what I want. The rest is the same as hers.
regex = r'(?<![a-zA-Z])\*{2,}(?![a-zA-Z])'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With