Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace character with another only if repeated and not part of a word

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?

like image 549
bongbang Avatar asked Mar 28 '19 02:03

bongbang


People also ask

How do I replace a character with another character?

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.

How do you replace a specific character in a string in Python?

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.

How do I replace multiple characters in a string?

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.

How do you replace multiple values in a string in Python?

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.


2 Answers

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)
like image 180
Danielle M. Avatar answered Oct 25 '22 11:10

Danielle M.


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])'
like image 26
bongbang Avatar answered Oct 25 '22 10:10

bongbang