Questions tagged [string-substitution]

Substituting string excerpts via sed, awk and other utilities.

220 questions
5
votes
2 answers

Why does String::sub!() change the original of a cloned object in Ruby?

I have a struct in my Ruby code that looks somewhat like this Parameter = Struct.new(:name, :id, :default_value, :minimum, :maximum) later, I create an instance of this struct using freq = Parameter.new('frequency', 15, 1000.0, 20.0, 20000.0) At…
bastibe
  • 16,551
  • 28
  • 95
  • 126
5
votes
2 answers

R - Remove dashes from a column with phone numbers

I'd like to create a new column of phone numbers with no dashes. I have data that is a mix of just numbers and some numbers with dashes. The data looks as follows: Phone 555-555-5555 1234567890 555-3456789 222-222-2222 51318312491
user3922483
  • 183
  • 2
  • 2
  • 8
4
votes
2 answers

Find and replace text between two strings in R

I have created some tutorials on R in some Rscripts. I need a Handout Set(HS) and a Coding Set (CS) without answers in which students can code . I need some help regex to search for the answer section in HO so I can remove it from the CS. In the HS …
WickHerd
  • 67
  • 7
4
votes
1 answer

JQ: How to multiply values that are recognised as strings?

I am trying to get some trade information from an exchange websocket. Both values .p and .q are enclosed between double quotes in the JSON I get from the socket. When I try to multiply two values, it says I am trying to multiply two strings. So I…
user11731610
4
votes
3 answers

Change text of an attributed string and retain attributes in Swift

For output in a database program, I have certain text that I've inserted marks to indicate bold or italics, as well as some text that is substituted for images. For instance: "%Important% ^All employees to the breakroom^" should have final output…
Dan M
  • 91
  • 1
  • 5
4
votes
2 answers

Cannot Figure out how String Substitution works in Python 3.x

In python 2.x you were allowed to do something like this: >>> print '%.2f' % 315.15321531321 315.15 However, i cannot get it to work for python 3.x, I tried different things, such as >>> print ('%.2f') % 315.15321531321 %.2f Traceback (most recent…
user2015601
3
votes
4 answers

How to use dynamic string substitution in Kotlin?

I'm looking for a Kotlin way to do a dynamic values substitution into a string. It is clear how to implement it, just want to check if there is something similar in standard library. Could you help me to find a function which given template and data…
diziaq
  • 6,881
  • 16
  • 54
  • 96
3
votes
4 answers

Pipe the output of basename to string substitution

I need the basename of a file that is given as an argument to a bash script. The basename should be stripped of its file extension. Let's assume $1 = "/somefolder/andanotherfolder/myfile.txt", the desired output would be "myfile". The current…
stee
  • 101
  • 7
3
votes
3 answers

Gsub a every element after a keyword in R

I'd like to remove all elements of a string after a certain keyword. Example : this.is.an.example.string.that.I.have Desired Output : This.is.an.example I've tried using gsub('string', '', list) but that only removes the word string. I've also…
eigenfoo
  • 229
  • 2
  • 9
3
votes
1 answer

Use vim to convert markdown headings to org headings

Use vim to convert a file of markdown headings to org heading. For example, change this: # heading one body ##heading two ### heading three body to this: * heading one body **heading two ****** heading six Only a continues sequence of '#' starting…
wolfv
  • 971
  • 12
  • 20
3
votes
4 answers

String manipulation of type String substitution in mathematical expression

Imagine something like exp(49/200)+(x-49/200) I want to pass as argument of the function "roundn" whatever operation that is not a addtion or a subtraction So my expression became roundn(exp(roundn(49/200, n)), n) + (x - roundn(49/200, n) Well the…
Peterstone
  • 7,119
  • 14
  • 41
  • 49
3
votes
3 answers

Return array of replacements from ruby

I want to take the string foofoofoo, map foo to bar, and return all individual replacements as an array - ['barfoofoo', 'foobarfoo', 'foofoobar'] This is the best I have: require 'pp' def replace(string, pattern, replacement) results = [] …
Anand
  • 3,690
  • 4
  • 33
  • 64
3
votes
2 answers

Java - extract key-value pairs from a string using a template

So there is this class called StrSubstitutor in org.apache.commons.lang3.text which can take in a map like this: Map params = new HashMap
Vivek V K
  • 1,100
  • 2
  • 15
  • 33
3
votes
1 answer

Regex for replacing printf-style calls with ostream left-shift syntax

The logging facility for our C++ project is about to be refactored to use repeated left-shift operators (in the manner of Qt's qDebug() syntax) instead of printf-style variadic functions. Suppose the logging object is called logger. Let's say we…
user1300886
3
votes
1 answer

How to replace brackets in Lua using string.gsub?

I have a function which is used to replace some words with a few characters or numbers. I am using string.gsub() function in this way: string.gsub(line, "[0-9%a%s/,-]+", "\t") This works very good with strings with numbers, letters, spaces, ,, and…
woyaru
  • 5,544
  • 13
  • 54
  • 92
1
2
3
14 15