How to search and replace text in a file?

How do I search and replace text in a file using Python 3? Here is my code: import os import sys import fileinput print (“Text to search for:”) textToSearch = input( “> ” ) print (“Text to replace it with:”) textToReplace = input( “> ” ) print (“File to perform Search-Replace on:”) fileToSearch = input( … Read more

Is there an alternative to string.Replace that is case-insensitive?

I need to search a string and replace all occurrences of %FirstName% and %PolicyAmount% with a value pulled from a database. The problem is the capitalization of FirstName varies. That prevents me from using the String.Replace() method. I’ve seen web pages on the subject that suggest Regex.Replace(strInput, strToken, strReplaceWith, RegexOptions.IgnoreCase); However for some reason when … Read more

Replace specific characters within strings

I would like to remove specific characters from strings within a vector, similar to the Find and Replace feature in Excel. Here are the data I start with: group <- data.frame(c(“12357e”, “12575e”, “197e18”, “e18947”) I start with just the first column; I want to produce the second column by removing the e‘s: group group.no.e 12357e … Read more

How to grep and replace

I need to recursively search for a specified string within all files and subdirectories within a directory and replace this string with another string. I know that the command to find it might look like this: grep ‘string_to_find’ -r ./* But how can I replace every instance of string_to_find with another string? 10 Answers 10

Java how to replace 2 or more spaces with single space in string and delete leading and trailing spaces

Looking for quick, simple way in Java to change this string ” hello there ” to something that looks like this “hello there” where I replace all those multiple spaces with a single space, except I also want the one or more spaces at the beginning of string to be gone. Something like this gets … Read more