Questions tagged [replace]

Replacing is the action of searching a string for a sub-string and replacing it with a different string.

Replacing is the action of searching a string (the haystack) for a sub-string (the needle) and replacing it with a different string.

for example, replacing all 'l' within 'Hello' with 'y', would result in 'Heyyo'.

27903 questions
192
votes
10 answers

Speed up millions of regex replacements in Python 3

I have two lists: a list of about 750K "sentences" (long strings) a list of about 20K "words" that I would like to delete from my 750K sentences So, I have to loop through 750K sentences and perform about 20K replacements, but ONLY if my words are…
pdanese
  • 2,187
  • 4
  • 15
  • 21
188
votes
8 answers

Case preserving substitute in Vim

Can this can be done in Vim? What I mean is: searching for 'BadJob' and replacing with 'GoodJob' would do the following replacements 'badjob' -> 'goodjob' 'BadJob' -> 'GoodJob' 'badJob' -> 'goodJob' 'BADJOB' -> 'GOODJOB'
davetapley
  • 17,000
  • 12
  • 60
  • 86
185
votes
5 answers

How can I use mySQL replace() to replace strings in multiple records?

We have a database that has a bunch of records with some bad data in one column, in which an embedded editor escaped some stuff that shouldn't have been escaped and it's breaking generated links. I want to run a query to replace the bad characters…
EmmyS
  • 11,892
  • 48
  • 101
  • 156
182
votes
7 answers

JavaScript .replace only replaces first Match

var textTitle = "this is a test" var result = textTitle.replace(' ', '%20'); But the replace functions stop at the first instance of the " " and I get the Result: "this%20is a test" Any ideas on where I'm going wrong I'm sure it's a simple fix.
Yardstermister
  • 2,041
  • 3
  • 15
  • 12
176
votes
7 answers

Replace None with NaN in pandas dataframe

I have table x: website 0 http://www.google.com/ 1 http://www.yahoo.com 2 None I want to replace python None with pandas NaN. I tried: x.replace(to_replace=None, value=np.nan) But I got: TypeError: 'regex' must be a string or a…
AdamNYC
  • 19,887
  • 29
  • 98
  • 154
173
votes
15 answers

Replace last occurrence of a string in a string

Anyone know of a very fast way to replace the last occurrence of a string with another string in a string? Note, the last occurrence of the string might not be the last characters in the string. Example: $search = 'The'; $replace = 'A'; $subject =…
Kirk Ouimet
  • 27,280
  • 43
  • 127
  • 177
173
votes
4 answers

Visual Studio, Find and replace, regex

I am trying to replace all the #include "whatever.h" with #include using find and replace functionality in Visual Studio 2005. I used the regex \#include \"[a-z\.h]+\" to find the include statement. But I am wondering how frame the…
bdhar
  • 21,619
  • 17
  • 70
  • 86
172
votes
7 answers

Replace values in list using Python

I have a list where I want to replace values with None where condition() returns True. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] For example, if condition checks bool(item%2) should return: [None, 1, None, 3, None, 5, None, 7, None, 9, None] What is the…
ak.
  • 2,422
  • 4
  • 21
  • 18
163
votes
25 answers

How can I replace two strings in a way that one does not end up replacing the other?

Let's say that I have the following code: String word1 = "bar"; String word2 = "foo"; String story = "Once upon a time, there was a foo and a bar." story = story.replace("foo", word1); story = story.replace("bar", word2); After this code runs, the…
Pikamander2
  • 7,332
  • 3
  • 48
  • 69
161
votes
10 answers

How to replace case-insensitive literal substrings in Java

Using the method replace(CharSequence target, CharSequence replacement) in String, how can I make the target case-insensitive? For example, the way it works right now: String target = "FooBar"; target.replace("Foo", "") // would return "Bar" String…
J. Lin
  • 2,259
  • 5
  • 20
  • 17
158
votes
7 answers

Replace and overwrite instead of appending

I have the following code: import re #open the xml file for reading: file = open('path/test.xml','r+') #convert to string: data =…
Kaly
  • 3,289
  • 4
  • 24
  • 25
151
votes
2 answers

Is there a way to find/replace across an entire project in Eclipse?

I'm trying to do a find and replace over many files within an Eclipse project, but I can't seem to find a way to do it. Googling showed me that there are plug-ins that can accomplish this, but is there any built-in functionality in Eclipse? (It…
froadie
  • 79,995
  • 75
  • 166
  • 235
150
votes
10 answers

Replace String in all files in Eclipse

How can I search and replace a String in all files of my current project? Let's say I have the string "/sites/default/" now I want it to be "/public/sites/default/", but there are almost 1000 files.
RickMx
  • 1,519
  • 2
  • 10
  • 4
150
votes
5 answers

How to search for occurrences of more than one space between words in a line

How to search for occurrences of more than one space between words in a line 1. this is a line containing 2 spaces 2. this is a line containing 3 spaces 3. this is a line containing multiple spaces first second three four All the above are…
Sam
  • 8,387
  • 19
  • 62
  • 97
147
votes
3 answers

JavaScript replace/regex

Given this function: function Repeater(template) { var repeater = { markup: template, replace: function(pattern, value) { this.markup = this.markup.replace(pattern, value); } }; return…
core
  • 32,451
  • 45
  • 138
  • 193