Problem Statement: If a special character is found with alphabets, then replace it with one space. And, if it is found with digits, then simply ignore.
Actual Scenario:
$45
4.5 inches
Task.This is good.
Hello, How $are you. This is good.
Expected Scenario:
$45
4.5 inches
Task This is good
Hello How are you This is good
I have tried writing one regex to find out the texts which follows this pattern, but not sure exactly how to replace special characters in that text with a space.
For e.g. In above image, expected output would be 'ddddd dfhghg'
, '222 d'
etc.
Can this scenario be handled by re.sub(pattern, replacement, input)? If yes, please tell how :)
For example, to keep all alphanumeric characters and spaces, we simply tell the . sub() method to replace anything except for [^a-zA-Z0-9 ] .
To replace text in a file we are going to open the file in read-only using the open() function. Then we will t=read and replace the content in the text file using the read() and replace() functions.
Replace all occurrences of a character in a string To replace all the occurrences of a character with a new character, we simply have to pass the old character and the new character as input to the replace() method when it is invoked on the string. You can observe this in the following example.
The string is immutable in Python. We can’t change a character in a string directly. So, we need to create one different string using the characters of the provided string. During the iteration process, keep building the new string by joining the characters. If any replaceable character is found, replace it with the symbol and join it.
The easiest way to get rid of multiple spaces in a string is with a regular expression search using the Python sub()function from the re module. We can easily define a regular expression which will search for any number of spaces, and then using the sub()function, we will replace the multiple spaces with one space.
Multiple spaces between words in a string of text can make sentences and text unreadable. We can easily replace multiple spaces with single spaces in a string in Python. The easiest way to get rid of multiple spaces in a string is with a regular expression search using the Python sub()function from the re module.
You can use a character set with wrapped with negative lookarounds:
(?<!\d)([.,$])(?!\d)
Put all the chars that should be replaced inside the brackets: [.,$]
Demo
Explanation:
(?<!\d)
negative lookbehind - ensure that there is no digit just before a matched character(?!\d)
negative lookahead - ensure that there is no digit just after a matched character[...]
character set with all the special chars you want to replaceIf 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