gsub

Why does String#gsub double content?

浪尽此生 提交于 2019-11-26 08:35:10
问题 s = \"#main= \'quotes\' s.gsub \"\'\", \"\\\\\'\" # => \"#main= quotes\'quotes\" This seems to be wrong, I expect to get \"#main= \\\\\'quotes\\\\\'\" when I don\'t use escape char, then it works as expected. s.gsub \"\'\", \"*\" # => \"#main= *quotes*\" So there must be something to do with escaping. Using ruby 1.9.2p290 I need to replace single quotes with back-slash and a quote. Even more inconsistencies: \"\\\\\'\".length # => 2 \"\\\\*\".length # => 2 # As expected \"\'\".gsub(\"\'\", \"

R - gsub replacing backslashes

烂漫一生 提交于 2019-11-26 08:28:23
问题 I would like to use gsub to replace every occurrence of a backslash in a string with 2 backslashes. Currently, what I have I tried is gsub(\"\\\\\\\\\", \"\\\\\", x) . This doesn\'t seem to work though. However, if I change the expression to instead replace each backslash with \"a\", it works fine. > gsub(\"\\\\\\\\\", \"\\\\\", \"\\\\\") [1] \"\" > gsub(\"\\\\\\\\\", \"a\", \"\\\\\") [1] \"a\" > gsub(\"\\\\\\\\\", \"\\\\\\\\\", \"\\\\\") [1] \"\\\\\" The last character is only a single

Extracting unique numbers from string in R

谁说胖子不能爱 提交于 2019-11-26 06:38:01
问题 I have a list of strings which contain random characters such as: list=list() list[1] = \"djud7+dg[a]hs667\" list[2] = \"7fd*hac11(5)\" list[3] = \"2tu,g7gka5\" I\'d like to know which numbers are present at least once ( unique() ) in this list. The solution of my example is: solution: c(7,667,11,5,2) If someone has a method that does not consider 11 as \"eleven\" but as \"one and one\", it would also be useful. The solution in this condition would be: solution: c(7,6,1,5,2) (I found this

Replace characters using gsub, how to create a function?

青春壹個敷衍的年華 提交于 2019-11-26 04:51:32
问题 I\'m trying to replace characters in a data.frame. I have a solution for this > df <- data.frame(var1 = c(\"aabbcdefg\", \"aabbcdefg\")) > df var1 1 aabbcdefg 2 aabbcdefg > df$var1 <- gsub(\"a\", \"h\", df$var1) > df$var1 <- gsub(\"b\", \"i\", df$var1) > df$var1 <- gsub(\"c\", \"j\", df$var1) > df$var1 <- gsub(\"d\", \"k\", df$var1) > df$var1 <- gsub(\"e\", \"l\", df$var1) > df$var1 <- gsub(\"f\", \"m\", df$var1) > df var1 1 hhiijklmg 2 hhiijklmg > but I would like to avoid using several gsub

Replace specific characters within strings

馋奶兔 提交于 2019-11-26 03:15:14
问题 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 column; I want to produce the second column by removing the e \'s: group group.no.e 12357e 12357 12575e 12575 197e18 19718 e18947 18947 回答1: With a regular expression and the function gsub() : group <- c("12357e", "12575e", "197e18", "e18947")

Replace multiple letters with accents with gsub

一个人想着一个人 提交于 2019-11-26 01:49:56
问题 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 line, right? I dont find the gsub help to be very comprehensive on this. 回答1: Use the character translation function chartr("áéó", "aeo", mydata) 回答2: An interesting question! I think the simplest option is to devise a special function, something like a

Replace multiple letters with accents with gsub

左心房为你撑大大i 提交于 2019-11-26 01:35:32
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 line, right? I dont find the gsub help to be very comprehensive on this. Use the character translation function chartr("áéó", "aeo", mydata) An interesting question! I think the simplest option is to devise a special function, something like a "multi" gsub(): mgsub <- function(pattern, replacement, x, ...) { if (length(pattern)!=length(replacement)) { stop("pattern and

How to use grep()/gsub() to find exact match

自闭症网瘾萝莉.ら 提交于 2019-11-26 00:49:08
问题 string = c(\"apple\", \"apples\", \"applez\") grep(\"apple\", string) This would give me the index for all three elements in string . But I want an exact match on the word \"apple\" (i.e I just want grep() to return index 1). 回答1: Use word boundary \b which matches a between a word and non-word character, string = c("apple", "apples", "applez") grep("\\bapple\\b", string) [1] 1 OR Use anchors. ^ Asserts that we are at the start. $ Asserts that we are at the end. grep("^apple$", string) [1] 1