Questions tagged [gsub]

Ruby, Lua, R and Awk methods for performing a global pattern substitution.

Ruby

The String#gsub method replaces all occurrences of a pattern (a regex or a plain string) with another string or the result of a block. gsub returns a copy of the original string with the replacements applied, gsub! performs that replacement in-place.

Lua

If used simply, it will replace all instances of provided with the replacement passed as argument. Instead, if the replacement is a function and not a string, the function receives all matched instances of the pattern as arguments. The string value; if returned, by the function is then substituted back in original string.

R

The function gsub replaces all occurrences of a pattern (a regex or a plain string) with another string or the result of a block. The function returns a new object; the original object is not modified. The function is vectorized and hence can be applied to vectors of strings too.

Awk

The function gsub(p, s [, t]) replaces all occurrences of a pattern p (a regex or a plain string) with another string s, either in the named target variable, t or in $0 if no target is given.

2445 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
93
votes
3 answers

gsub() in R is not replacing '.' (dot)

I want to replace dots in "2014.06.09" to "2014-06-09". I am using gsub() function for it. If x <- "2014.06.09" gsub('2', '-' ,x) # [1] "-014.06.09" But when I try gsub('.', '-', x) # [1] "----------" instead of "2014-06-09". class(x) #…
Zeeshan
  • 1,248
  • 1
  • 12
  • 19
88
votes
7 answers

Ruby multiple string replacement

str = "Hello☺ World☹" Expected output is: "Hello:) World:(" I can do this: str.gsub("☺", ":)").gsub("☹", ":(") Is there any other way so that I can do this in a single function call?. Something like: str.gsub(['s1', 's2'], ['r1', 'r2'])
Sayuj
  • 7,464
  • 13
  • 59
  • 76
72
votes
11 answers

Replace multiple letters with accents with gsub

of course I could replace specific arguments like this: mydata=c("á","é","ó") mydata=gsub("á","a",mydata) mydata=gsub("é","e",mydata) mydata=gsub("ó","o",mydata) mydata but surely there is a easier way to do this all in onle…
Joschi
  • 2,941
  • 9
  • 28
  • 36
57
votes
1 answer

Lua String replace

How would i do this? I got this: name = "^aH^ai" string.gsub(name, "^a", "") which should return "Hi", but it grabs the caret character as a pattern character What would be a work around for this? (must be done in gsub)
Frank
  • 573
  • 1
  • 4
  • 4
56
votes
5 answers

Ruby post title to slug

How should I convert a post title to a slug in Ruby? The title can have any characters, but I only want the slug to allow [a-z0-9-_] (Should it allow any other characters?). So basically: downcase all letters convert spaces to hyphens delete…
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
49
votes
3 answers

Remove parenthesis from a character string

I am trying to remove a parenthesis from a string in R and run into the following error: string <- "log(M)" gsub("log", "", string) # Works just fine gsub("log(", "", string) #breaks # Error in gsub("log(", "", test) : # invalid regular…
mike
  • 22,931
  • 31
  • 77
  • 100
45
votes
10 answers

Removing Whitespace From a Whole Data Frame in R

I've been trying to remove the white space that I have in a data frame (using R). The data frame is large (>1gb) and has multiple columns that contains white space in every data entry. Is there a quick way to remove the white space from the whole…
Thirst for Knowledge
  • 1,606
  • 2
  • 26
  • 43
42
votes
10 answers

Replace multiple strings in one gsub() or chartr() statement in R?

I have a string variable containing alphabet[a-z], space[ ], and apostrophe['],eg. x <- "a'b c" I want to replace apostrophe['] with blank[], and replace space[ ] with underscore[_]. x <- gsub("'", "", x) x <- gsub(" ", "_", x) It works absolutely,…
Eric Chang
  • 2,580
  • 4
  • 19
  • 19
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
36
votes
4 answers

Remove pattern from string with gsub

I am struggling to remove the substring before the underscore in my string. I want to use * (wildcard) as the bit before the underscore can vary: a <- c("foo_5", "bar_7") a <- gsub("*_", "", a, perl = TRUE) The result should look like: > a [1] 5…
user969113
  • 2,349
  • 10
  • 44
  • 51
32
votes
4 answers

Remove all punctuation except apostrophes in R

I'd like to use R's gsub to remove all punctuation from a text except for apostrophes. I'm fairly new to regex but am learning. Example: x <- "I like %$@to*&, chew;: gum, but don't like|}{[] bubble@#^)( gum!?" gsub("[[:punct:]]", "",…
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
32
votes
1 answer

Using named capture groups inside Ruby gsub blocks (regex)

I'm trying to use a named capture group inside a block in Ruby. $1 still works, but I'd like to reference it using the name I gave. "foo /(bar)".gsub(/(? \(.*?\) )/x) do |match| puts "$1 = #{$1} and $my_word =…
Chris
  • 2,174
  • 28
  • 37
31
votes
1 answer

How can I remove non-numeric characters from strings using gsub in R?

I use the gsub function in R to remove unwanted characters in numbers. So I should remove from the strings every character that is not a number, ., and -. My problem is that the regular expression is not removing some non-numeric characters like d,…
jair.jr
  • 597
  • 1
  • 7
  • 13
30
votes
3 answers

Ruby gsub doesn't escape single-quotes

I don't understand what is going on here. How should I feed gsub to get the string "Yaho\'o"? >> "Yaho'o".gsub("Y", "\\Y") => "\\Yaho'o" >> "Yaho'o".gsub("'", "\\'") => "Yahooo"
user263888
1
2 3
99 100