Questions tagged [string-substitution]

Substituting string excerpts via sed, awk and other utilities.

220 questions
332
votes
7 answers

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…
Luke
  • 4,769
  • 5
  • 28
  • 36
37
votes
1 answer

Ruby match first occurrence of string for a gsub replacement

I have a string let's say http://someUrul.com/someController/SOmeAction?SomeQS=http://someOtherUrl and I want to replace the first http with https, but not the second, so I end up with…
ar3
  • 3,883
  • 3
  • 21
  • 22
21
votes
2 answers

How to use ruby gsub Regexp with many matches?

I have csv file contents having double quotes inside quoted text test,first,line,"you are a "kind" man",thanks again,second,li,"my "boss" is you",good I need to replace every double quote not preceded or succeeded by a comma by…
Mahmoud Khaled
  • 6,226
  • 6
  • 37
  • 42
16
votes
2 answers

Perl6 search then replace with output of subroutine

I've combed the docs but I can't seem to find how to do this in perl6. In perl5 I would have done (just an example): sub func { ... } $str =~ s/needle/func($1)/e; i.e. to replace 'needle' with the output of a call to 'func'
iPherian
  • 908
  • 15
  • 38
12
votes
3 answers

Bash or-equals ||= like Ruby

Does Bash have something like ||= ? I.e., is there a better way to do the following: if [ -z $PWD ]; then PWD=`pwd`; fi I'm asking because I get this error: $ echo ${`pwd`/$HOME/'~'} -bash: ${`pwd`/$HOME/'~'}: bad substitution So, my plan is to…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
10
votes
1 answer

String replacement with .subst in a for loop

I'd like to make a string substitution in a for block using a named capture. I've expected to get the numbers 1,2,3 as output. But it is Nil for the first run, and then 1 and 2 for the 2nd and 3rd run. How do I use the .subst correctly in the loop…
LuVa
  • 2,288
  • 2
  • 10
  • 20
8
votes
2 answers

How can I compare two strings to find the number of characters that match in R, using substitution distance?

In R, I have two character vectors, a and b. a <- c("abcdefg", "hijklmnop", "qrstuvwxyz") b <- c("abXdeXg", "hiXklXnoX", "Xrstuvwxyz") I want a function that counts the character mismatches between each element of a and the corresponding element…
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
7
votes
3 answers

regex for replacement of specific character outside parenthesis only

I am looking for regex (preferably in R) which can replace (any number of) specific characters say ; with say ;; but only when not present inside parenthesis () inside the text string. Note: 1. There may be more than one replacement character…
AnilGoyal
  • 25,297
  • 4
  • 27
  • 45
7
votes
6 answers

awk dynamic document indexing

I have a document that I need to dynamically create/update the indexes in. I am trying to acomplish this with awk. I have a partial working example but now I'm stumped. The example document is as follows. numbers.txt: #) Title #) Title …
Datavar
  • 71
  • 2
6
votes
4 answers

sed wildcard substitution

I want to do a substitution based on a wildcard. For example, change all "tenure" to "disposition" only if the word "tenure" comes after an '=' sign. Basically a regex that would match this =.*tenure The sed command that I have so for this is: sed…
Steve
  • 11,831
  • 14
  • 51
  • 63
6
votes
1 answer

Different behavior of base R gsub and stringr::str_replace_all?

I would expect gsub and stringr::str_replace_all to return the same result in the following, but only gsub returns the intended result. I am developing a lesson to demonstrate str_replace_all so I would like to know why it returns a different result…
qdread
  • 3,389
  • 19
  • 36
6
votes
1 answer

Add leading zero within a character string

One column of my data.frame looks like the following: c("BP_1_CSPP", "BP_2_GEGS", "BP_3_AEAG", "BP_4_KPAP", "BP_5_TAKP", "BP_6_GGDR", "BP_7_MQQP", "BP_8_EEEE", "BP_9_RSDP", "BP_10_APAS", "BP_11_KRGG", "BP_12_RSQQ", "BP_13_QQLS", "BP_14_EPEV",…
BCArg
  • 2,094
  • 2
  • 19
  • 37
6
votes
2 answers

Substitute Function in Excel VBA for cell range

I have to replace one character with another in Excel file. I have used following Replace function, but due to exceeds of 1024 character limit in some cells, it stops there. Sub Replace() With Sheets("Sheet1").Range("A1:A629") …
Shabar
  • 2,617
  • 11
  • 57
  • 98
5
votes
3 answers

Shorter way to remove non-characters than gsub(/\d|\W/, "")

my_string = 'Here's the #: 49848! - but will dashes, commas & stars (*) show?' puts src.gsub(/\d|\W/, "") i.e. can I remove the or ("|"). Here's how I got here, can I get shorter? src = "Here's the #: 49848! - but will dashes, commas & stars (*)…
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
5
votes
1 answer

What is the JS equivalent to the Ruby :tr method?

In ruby I could do this: def DNA_strand(dna) base_hash = { 'A' => 'T', 'T' => 'A', 'G' => 'C', 'C' => 'G' } dna.gsub(/[ATGC]/, base_hash) end I could also do this which would be exactly equivalent: def DNA_strand(dna) Dna.tr(’ACTG’,…
Richard Jarram
  • 899
  • 11
  • 22
1
2 3
14 15