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
248
votes
7 answers

Visual Studio Clicking Find Results Opens Code in Wrong Window

I'm using Visual Studio 2010 and when I do a "Find in Files" the results are returned to the "Find Results 1" window which is docked below my code editor window. Before, I would double click on one of the results in the Find Results window and the…
JPM
  • 2,481
  • 2
  • 14
  • 3
240
votes
8 answers

Replace only some groups with Regex

Let's suppose I have the following regex: -(\d+)- and I want to replace, using C#, the Group 1 (\d+) with AA, to obtain: -AA- Now I'm replacing it using: var text = "example-123-example"; var pattern = @"-(\d+)-"; var replaced =…
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124
236
votes
11 answers

How to remove all characters after a specific character in python?

I have a string. How do I remove all text after a certain character? (In this case ...) The text after will ... change so I that's why I want to remove all characters after a certain one.
Solihull
  • 6,779
  • 5
  • 22
  • 12
233
votes
7 answers

How to replace text in a string column of a Pandas dataframe?

I have a column in my dataframe like this: range "(2,30)" "(50,290)" "(400,1000)" ... and I want to replace the , comma with - dash. I'm currently using this method but nothing is changed. org_info_exc['range'].replace(',', '-', inplace=True) Can…
UserYmY
  • 8,034
  • 17
  • 57
  • 71
232
votes
20 answers

Removing a list of characters in string

I want to remove characters in a string in python: string.replace(',', '').replace("!", '').replace(":", '').replace(";", '')... But I have many characters I have to remove. I thought about a list list = [',', '!', '.', ';'...] But how can I use…
Laura
  • 4,199
  • 6
  • 23
  • 24
224
votes
6 answers

Find and replace - Add carriage return OR Newline

In the case of following string to be parsed. ford mustang,10,blue~~?bugatti veyron,13,black I want to replace the ~~? with a carriage return Replacing with \n just adds the string "\n" How can this be done?
Mantisimo
  • 4,203
  • 5
  • 37
  • 55
213
votes
3 answers

jQuery - replace all instances of a character in a string

This does not work and I need it badly $('some+multi+word+string').replace('+', ' ' ); always gets some multi+word+string it's always replacing for the first instance only, but I need it to work for all + symbols.
thednp
  • 4,401
  • 4
  • 33
  • 45
210
votes
21 answers

How to replace part of string by position?

I have this string: ABCDEFGHIJ I need to replace from position 4 to position 5 with the string ZX It will look like this: ABCZXFGHIJ But not to use with string.replace("DE","ZX") - I need to use with position How can I do it?
Gali
  • 14,511
  • 28
  • 80
  • 105
210
votes
11 answers

How to replace an entire line in a text file by line number

I have a situation where I want a bash script to replace an entire line in a file. The line number is always the same, so that can be a hard-coded variable. I'm not trying to replace some sub-string in that line, I just want to replace that line…
user788171
  • 16,753
  • 40
  • 98
  • 125
209
votes
8 answers

How do I perform a Perl substitution on a string while keeping the original?

In Perl, what is a good way to perform a replacement on a string using a regular expression and store the value in a different variable, without changing the original? I usually just copy the string to a new variable then bind it to the s/// regex…
kaybenleroll
  • 16,794
  • 16
  • 54
  • 66
201
votes
4 answers

What flavor of Regex does Visual Studio Code use?

Trying to search-replace in Visual Studio Code, I find that its Regex flavor is different from full Visual Studio. Specifically, I try to declare a named group with string (?

[\w]+) which works in Visual Studio, but not in Visual Studio Code.…

Eric
  • 2,797
  • 2
  • 20
  • 19
201
votes
6 answers

How to replace a single word under cursor?

How do I replace a word under the cursor in Vim. So instead of using dw then i then the word and then Esc, is there a simpler combination to replace the word under the cursor?
sayth
  • 6,696
  • 12
  • 58
  • 100
199
votes
7 answers

Conditional Replace Pandas

I have a DataFrame, and I want to replace the values in a particular column that exceed a value with zero. I had thought this was a way of achieving this: df[df.my_channel > 20000].my_channel = 0 If I copy the channel into a new data frame it's…
BMichell
  • 3,581
  • 5
  • 23
  • 31
198
votes
10 answers

Replace all non alphanumeric characters, new lines, and multiple white space with one space

I'm looking for a neat regex solution to replace All non alphanumeric characters All newlines All multiple instances of white space With a single space For those playing at home (the following does work) text.replace(/[^a-z0-9]/gmi, "…
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
197
votes
13 answers

How to remove line breaks (no characters!) from the string?

This might appear to be a dupe, but rest assured it isn't - I have searched both SO as well as the rest of the web for an answer to my problem and ended up finding the same insufficient "solutions" over and over. Anyhow, here it goes: I'm saving…
Johannes Pille
  • 4,073
  • 4
  • 26
  • 27