gsub

Ruby gsub doesn't escape single-quotes

柔情痞子 提交于 2019-11-27 08:36:15
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" \' means $' which is everything after the match. Escape the \ again and it works "Yaho'o".gsub("'", "\\\\'") "Yaho'o".gsub("'", "\\\\'") Because you're escaping the escape character as well as escaping the single quote. This will also do it, and it's a bit more readable: def escape_single_quotes(str) str.gsub(/'/) { |x| "\\#{x}" } end If you want to escape both a single-quote and a backslash, so that you can embed that

Writing R function with if enviornment

a 夏天 提交于 2019-11-27 08:27:04
问题 I am trying to write a function which does different things, depending on the second argument. But I am getting an error all the time. Depending on the dimension of the matrix, the function should perform different tasks. Here is an example x<-cbind(X1,X2,X3) function<-function(x,hnrstr){ if (hnrstr<-1){ x<-data.frame(X1=x[1],X2=x[2],X3=x[3]) y<-x y[ ,"X2","X3"]<- gsub(" {2, }"," ",y[ ,"X2","X3"]) } if (hnrstr<-2){ x<-data.frame(X1=x[1],X2=x[2]) P<-x } if (hnrstr<-1){ x<-y } if (hnrstr<-2){ x

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

耗尽温柔 提交于 2019-11-27 07:58:06
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) # "character" Can some suggest me a way to get this right and also why it is not working for '.' (dot) You may need to escape the . which is a special character that means "any character" (from @Mr Flick's comment) gsub('\\.', '-', x) #[1] "2014-06-09" Or gsub('[.]', '-', x) #[1] "2014-06-09" Or as @Moix mentioned in the comments, we can also use fixed=TRUE

Replace first occurrence of “:” but not second in R

孤者浪人 提交于 2019-11-27 07:38:21
问题 In order to be able to process I'd like to replace the first occurrence of a : in a string (which is my marker, that a speech begins). text <- c("Mr. Mark Francois (Rayleigh) (Con): If the scheme was so poorly targeted, why were the Government about to roll it out to employees in the Department of Trade and Industry and the Department for Work and Pensions on the very day the Treasury scrapped it? The CBI and the TUC have endorsed the scheme, which has helped 500,000 people and their families

R remove multiple text strings in data frame

我的梦境 提交于 2019-11-27 07:11:25
问题 New to R. I am looking to remove certain words from a data frame. Since there are multiple words, I would like to define this list of words as a string, and use gsub to remove. Then convert back to a dataframe and maintain same structure. wordstoremove <- c("ai", "computing", "ulitzer", "ibm", "privacy", "cognitive") a id text time username 1 "ai and x" 10 "me" 2 "and computing" 5 "you" 3 "nothing" 15 "everyone" 4 "ibm privacy" 0 "know" I was thinking something like: a2 <- apply(a, 1, gsub

Remove everything after space in string

一个人想着一个人 提交于 2019-11-27 06:03:34
问题 I would like to remove everything after a space in a string. For example: "my string is sad" should return "my" I've been trying to figure out how to do this using sub/gsub but have been unsuccessful so far. 回答1: strsplit("my string is sad"," ")[[1]][1] 回答2: or, substitute everything behind the first space to nothing: gsub(' [A-z ]*', '' , 'my string is sad') And with numbers: gsub('([0-9]+) .*', '\\1', c('c123123123 0320.1')) 回答3: You may use a regex like sub(" .*", "", x) See the regex demo

R - gsub replacing backslashes

余生长醉 提交于 2019-11-27 04:49:12
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 backslash; R just prints 2 because it prints escaped characters with the backslash. Using nchar confirms that the length is 1. What causes this functionality? The second

Extract part of string (till the first semicolon) in R

跟風遠走 提交于 2019-11-27 03:30:30
问题 I have a column containing values of 3 strings separated by semicolons. I need to just extract the first part of the string. Type <- c("SNSR_RMIN_PSX150Y_CSH;SP_12;I0.00V50HX0HY3000") What I want is: Get the first part of the string (till the first semicolon). Output : SNSR_RMIN_PSX150Y_CSH I tried gsub but not able to understand. Kindly let me know how we can do this efficiently in R. 回答1: You could try sub sub(';.*$','', Type) #[1] "SNSR_RMIN_PSX150Y_CSH" It will match the pattern i.e.

Remove pattern from string with gsub

给你一囗甜甜゛ 提交于 2019-11-27 01:26:00
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 7 I also tried stuff like "^* " or "? " but did not really work. The following code works on your example : gsub(".*_", "", a) Alternatively, you can also try: gsub("\\S+_", "", a) Praveen as.numeric(gsub(pattern=".*_", replacement = '', a) [1] 5 7 来源: https://stackoverflow.com/questions/11776287/remove-pattern-from-string-with-gsub

Why does String#gsub double content?

半腔热情 提交于 2019-11-26 23:04:51
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("'", "\\*").length # => 2 "'a'".gsub("'", "\\*") # => "\\*a\\*" (length==5) # WTF next: "'".gsub("'", "\\'").length # => 0 # Doubling the content? "'a'"