gsub

Extract URL parameters and values in R

╄→尐↘猪︶ㄣ 提交于 2019-12-10 19:37:04
问题 We want to extract parameters and values from a given URL like http://www.exemple.com/?a=1&b=2&c=3#def Using xml2::url_parse we were able to Parse a url into its component pieces. However we still want to devide the query into elements using gsub matching regular expression: ([^?&=#]+)=([^&#]*) Desired output a=1 b=2 c=3 回答1: We can try library(stringr) matrix(str_extract_all(str1, "[a-z](?=\\=)|(?<=\\=)\\d+")[[1]], ncol=2, byrow=TRUE) Or if we need the = also str_extract_all(str1, "[a-z]=\\d

How to understand gsub(/^.*\//, '') or the regex

ⅰ亾dé卋堺 提交于 2019-12-10 19:00:32
问题 Breaking up the below code to understand my regex and gsub understanding: str = "abc/def/ghi.rb" str = str.gsub(/^.*\//, '') #str = ghi.rb ^ : beginning of the string \/ : escape character for / ^.*\/ : everything from beginning to the last occurrence of / in the string Is my understanding of the expression right? How does .* work exactly? 回答1: Your general understanding is correct. The entire regex will match abc/def/ and String#gsub will replace it with empty string. However, note that

Escaping '“' with regular double quotes using Ruby regex

坚强是说给别人听的谎言 提交于 2019-12-10 18:31:26
问题 I have text that has these fancy double quotes: '“' and I would like to replace them with regular double quotes using Ruby gsub and regex. Here's an example and what I have so far: sentence = 'This is a quote, “Hey guys!”' I couldn't figure out how to escape double quotes so I tried using 34.chr: sentence.gsub("“",34.chr). This gets me close but leaves a back slash in front of the double quote: sentence.gsub("“",34.chr) => 'This is a quote, \"Hey guys!”' 回答1: The backslashes are only showing

R: need to replace invisible/accented characters with regex

我与影子孤独终老i 提交于 2019-12-10 18:25:27
问题 I'm working with a file generated from several different machines that had different locale-settings, so I ended up with a column of a data frame with different writings for the same word: CÓRDOBA CÓRDOBA CÒRDOBA I'd like to convert all those to CORDOBA . I've tried doing t<-gsub("Ó|Ó|Ã’|°|°|Ò","O",t,ignore.case = T) # t is the vector of names Wich works until it finds some "invisible" characters: As you can see, I'm not able to see, in R, the additional charater that lies between à and \

Concatenate gsub [duplicate]

那年仲夏 提交于 2019-12-10 18:14:48
问题 This question already has answers here : Replace multiple letters with accents with gsub (11 answers) Closed 6 years ago . I'm currently running the following code to clean my data from accent characters: df <- gsub('Á|Ã', 'A', df) df <- gsub('É|Ê', 'E', df) df <- gsub('Í', 'I', df) df <- gsub('Ó|Õ', 'O', df) df <- gsub('Ú', 'U', df) df <- gsub('Ç', 'C', df) However, I would like to do it in just one line (using another function for it would be ok). How can I do this? 回答1: Try something like

How to remove + (plus sign) from string in R?

别来无恙 提交于 2019-12-10 17:31:37
问题 Say I use gsub and want to remove the following (=,+,-) sign from the string and replace with an underscore. Can someone describe what is going on when I try to use the gsub with a plus sign (+). test<- "sandwich=bread-mustard+ketchup" # [1] "sandwich=bread-mustard+ketchup" test<-gsub("-","_",test) # [1] "sandwich=bread_mustard+ketchup" test<-gsub("=","_",test) # [1] "sandwich_bread_mustard+ketchup" test<-gsub("+","_",test) #[1] "_s_a_n_d_w_i_c_h___b_r_e_a_d___m_u_s_t_a_r_d_+_k_e_t_c_h_u_p_"

Spliting the character into parts

邮差的信 提交于 2019-12-10 16:46:48
问题 I observe the following character: l <- "mod, range1 = seq(-m, n, 0.1), range2 = seq(-2, 2, 0.1), range3 = seq(-2, 2, 0.1)" Using regular expressions in R I desire to split l in the following structure: [1] "mod" "range1 = seq(-m, n, 0.1)" [3] "range2 = seq(-2, 2, 0.1)" "range3 = seq(-2, 2, 0.1)" Unfortunetely, I didn't find a proper way to overcome the problem, yet. Anyone has an idea how is it possible to acquire such an elegeant split? 回答1: I really doubt you can do it with regular

smarter character replacement using ruby gsub and regexp

回眸只為那壹抹淺笑 提交于 2019-12-10 15:53:39
问题 I'm trying to create permalink like behavior for some article titles and i don't want to add a new db field for permalink. So i decided to write a helper that will convert my article title from: " O "focoasă" a pornit cruciada, împotriva bărbaţilor zgârciţi " to " o-focoasa-a-pornit-cruciada-impotriva-barbatilor-zgarciti ". While i figured out how to replace spaces with hyphens and remove other special characters (other than -) using: title.gsub(/\s/, "-").gsub(/[^\w-]/, '').downcase I am

Trouble with gsub and regex in R

末鹿安然 提交于 2019-12-10 14:44:16
问题 I am using gsub in R to add text into the middle of a string. It works perfectly but for some reason, when the location gets too long it throws an error. The code is below: gsub(paste0('^(.{', as.integer(loc[1])-1, '})(.+)$'), new_cols, sql) Error in gsub(paste0("^(.{273})(.+)$"), new_cols, sql) : invalid regular expression '^(.{273})(.+)$', reason 'Invalid contents of {}' This code works fine when the number in the brackets(273 in this case) is less but not when it is this large. This

gsub error turning upper to lower case in R

一曲冷凌霜 提交于 2019-12-10 13:47:52
问题 I would like to recode some identifiers, from upper case to lower case. I am not sure what the issue is here. n = c('AFD.434', 'BSD.23', 'F234.FF') gsub(pattern = '[[:upper:]]', replacement = '[[:lower:]]', n) [1] "[[:lower:]][[:lower:]][[:lower:]].434" "[[:lower:]][[:lower:]][[:lower:]].23" "[[:lower:]]234.[[:lower:]][[:lower:]]" Any advice? 回答1: Your gsub call replaces each occurrence with the literal string "[[:lower:]]". The simplest solution is to not use regular expressions; simply use