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
6
votes
1 answer

Powershell variable in replacement string with named groups

The following Powershell replace operation with named groups s1 and s2 in regex (just for illustration, not a correct syntax) works fine : $s -Replace "(?....)(?...)" '${s2}xxx${s1}' My question is : how to replace with a variable $x…
user957479
  • 491
  • 1
  • 5
  • 20
6
votes
3 answers

Replace a word from a string

I have mongodb documents with a field like this: Image : http://static14.com/p/Inc.5-Black-Sandals-5131-2713231-7-zoom.jpg How can I replace the zoom part in the string value with some other text in order to get: Image :…
Sameer Shaikh
  • 273
  • 1
  • 9
  • 21
6
votes
2 answers

String replacement on a whole text file in Python 3.x?

How can I replace a string with another string, within a given text file. Do I just loop through readline() and run the replacement while saving out to a new file? Or is there a better way? I'm thinking that I could read the whole thing into…
John B
  • 20,062
  • 35
  • 120
  • 170
6
votes
3 answers

Replace text in file with Python

I'm trying to replace some text in a file with a value. Everything works fine but when I look at the file after its completed there is a new (blank) line after each line in the file. Is there something I can do to prevent this from happening. Here…
Aaron
  • 2,672
  • 10
  • 28
  • 45
6
votes
1 answer

Why doesn't itertools.combination_with_replacement give 27 results for length-3 combinations of 3 values?

Below is my code import itertools a = [1,2,3] for i in itertools.combination_with_replacement(a,3): print i Output (1, 1, 1),(1, 1, 2) (1, 1, 3),(1, 2, 2) (1, 2, 3),(1, 3, 3) (2, 2, 2),(2, 2, 3) (2, 3, 3),(3, 3, 3) Only 10 result is print out,…
Math
  • 81
  • 1
  • 2
  • 5
6
votes
1 answer

AngularJS replace using regex on template

I have the following markup that shows a value from ng-model: {{document.content.replace(/\d+\_/,"")}} Before each document.content I add a number and an underscore, something like "12122141_document.txt". I…
Alex Arvanitidis
  • 4,403
  • 6
  • 26
  • 36
6
votes
2 answers

Resolve bash variable containted in another variable

I have code like that: TEXT_TO_FILTER='I would like to replace this $var to proper value in multiline text' var=variable All I want to get is: TEXT_AFTER_FILTERED="I'd like to replace this variable to proper value" So I…
kokosing
  • 5,251
  • 5
  • 37
  • 50
6
votes
4 answers

C# replace all occurrences of a character with just a character

Let's say I have this string, "%%%%%ABC", I would like to replace all the "%" with just one "%", so it should be "%ABC". If its "%%%A%%B%%C%%", it should be "%A%B%C%" How should I do this?
Dhinnesh Jeevan
  • 489
  • 2
  • 7
  • 22
6
votes
3 answers

Replacing last occurrence of substring in string

How can you replace the last occurrence of a substring in a string?
nickm
  • 119
  • 1
  • 1
  • 8
6
votes
3 answers

Find and Replace in AppleScript

I'm currently writing a super simple script and I need to find and replace text in a variable (specifically in the path of the items dropped onto the applescript.) Here is what I have currently: on open {dropped_items} tell application…
JohnShafto
  • 83
  • 1
  • 1
  • 4
6
votes
2 answers

Can I send Ant 'replace' task output to a new file?

The Ant replace task does an in-place replacement without creating a new file. The below snippet replaces tokens in any of the '*.xml' files with the corresponding values from the 'my.properties' file.
Sathish
  • 61
  • 1
  • 3
6
votes
4 answers

Substring substitution in bash

my problem of today is to replace in a string like this --> 6427//6422 6429//6423 6428//6421 every // with a ,. I tried with different commands: finalString=${startingString//[//]/,} doesn't work fileTemp=$(echo -e "$line\n" | tr "//" "," does a…
Mattia Baldari
  • 567
  • 1
  • 5
  • 15
6
votes
6 answers

Best way to remove thousand separators from string amount using a regex

I have variables that contain amounts and would like to remove the (US) thousand separators but also have to cover the scenario that there may be non-US formatted amounts where the comma is used for the decimals instead of for the thousands where I…
user2571510
  • 11,167
  • 39
  • 92
  • 138
6
votes
1 answer

re.sub replaces only first two instances

I have found this interesting issue with re.sub: import re s = "This: is: a: string:" print re.sub(r'\:', r'_', s, re.IGNORECASE) >>>> This_ is_ a: string: Notice how only the first two instances were replaced. It seems that adding the…
dwkd
  • 2,716
  • 1
  • 17
  • 17
6
votes
1 answer

VIM search and replace whole word starting with dollar

How can I search and replace whole words starting with dollar sign ($) in VIM? I know that \ matches the whole word and \$ escapes the dollar sign for regular search and replace, but using \<\$word\> or \$word\> only results in "E486: Pattern…
Scarabol
  • 63
  • 3