gsub

Ruby regex matching a line in an inputted text file string [duplicate]

拜拜、爱过 提交于 2019-12-06 09:26:28
This question already has answers here : Ruby regex gsub a line in a text file (5 answers) Closed 6 years ago . I need to match a line in an inputted text file string and wrap that captured line with a character for example. For example imagine a text file as such: test foo test bar I would like to use gsub to output: XtestX XfooX XtestX XbarX I'm having trouble matching a line though. I've tried using regex starting with ^ and ending with $, but it doesn't seem to work? Any ideas? I have a text file that has the following in it: test foo test bag The text file is being read in as a command

extracting values from column using tidyr

时光毁灭记忆、已成空白 提交于 2019-12-06 08:36:44
问题 I have data.frame annot defined as: annot <- structure(list(Name = c("dd_1", "dd_2", "dd_3","dd_4", "dd_5", "dd_6","dd_7"), GOs = c("C:extracellular space; C:cell body; P:cell migration process; P:NF/ß pathway", "C:Signal transduction; C:nucleus; F:positive regulation; P:single organism; P:positive(+) regulation", "C:cardiomyceltes; C:intracellular pace; F:putative; F:magnesium ion binding; F:calcium ion binding; P:visual perception; P:blood coagulation", "F:poly(A) RNA binding; P:DNA

R gsub to extract emails from text

女生的网名这么多〃 提交于 2019-12-06 06:48:36
I have a variable a created by readLines of a file which contains some emails. I already filtered only those rows whith the @ symbol, and now am struggling to grab the emails. The text in my variable looks like this: > dput(a[1:5]) c("buenas tardes. excelente. por favor a: Saolonm@hotmail.com", "26.leonard@gmail.com ", "Aprecio tu aporte , mi correo es jcdavola31@gmail.com , Muchas Gracias", "gracias andrescarnederes@headset.cl", "Me apunto, muchas gracias mi dirección luciana.chavela.ecuador@gmail.com me será de mucha utilidad. " ) From this question in SO I got a starting point to extract

Matching position in gsub or scan

走远了吗. 提交于 2019-12-06 03:55:44
What is the best way to achieve the matching position (the index that would be returned by =~ ) for each match when using gsub or scan ? "hello".gsub(/./) { Regexp.last_match.offset(0).first } => "01234" See Regexp.last_match and MatchData . I came to this problem from a different direction, and could not come up with a decent solution (that is, understandable, maintainable) to do this with either gsub or scan (both built-in methods of String class). So I asked "Why do it this way?..." and looked for more natural alternatives (thanks to Nash for pointing the general direction!): #!/usr/bin/env

Removing/replacing brackets from R string using gsub

↘锁芯ラ 提交于 2019-12-05 18:27:27
I want to remove or replace brackets "(" or ")" from my string using gsub. However as shown below it is not working. What could be the reason? > k<-"(abc)" > t<-gsub("()","",k) > t [1] "(abc)" Using the correct regex works: gsub("[()]", "", "(abc)") The additional square brackets mean "match any of the characters inside" . The possible way could be (in the line OP is trying) as: gsub("\\(|)","","(abc)") #[1] "abc" `\(` => look for `(` character. `\` is needed as `(` a special character. `|` => OR condition `)` = Look for `)` Or safe and simple solution that doesn't rely on regex: k <- gsub("("

pik Error: private method `gsub' called for nil:NilClass

≡放荡痞女 提交于 2019-12-05 18:00:18
I´m getting an error when adding JRuby 1.3.1 to pik. Error: Error: private method `gsub' called for nil:NilClass this is my path environment variable: C:\Users\Owner>echo %path% C:\Program Files\Java\jdk1.7.0_05\bin;c:\jruby-1.7.0.preview1\bin;C:\jruby-1.3.1\bin;c:\pik this is when trying to add 1.3.1 version: C:\Users\Owner>pik add C:\jruby-1.3.1\bin There was an error. Error: private method `gsub' called for nil:NilClass in: pik/commands/command.rb:124:in `get_version' in: pik/commands/add_command.rb:23:in `add' in: pik/commands/add_command.rb:13:in `execute' in: pik_runner:33 It´s not the

In regex, mystery Error: assertion 'tree->num_tags == num_tags' failed in executing regexp: file 'tre-compile.c', line 634

做~自己de王妃 提交于 2019-12-05 16:14:11
Assume 900+ company names pasted together to form a regex pattern using the pipe separator -- "firm.pat". firm.pat <- str_c(firms$firm, collapse = "|") With a data frame called "bio" that has a large character variable (250 rows each with 100+ words) named "comment", I would like to replace all the company names with blanks. Both a gsub call and a str_replace_all call return the same mysterious error. bio$comment <- gsub(pattern = firm.pat, x = bio$comment, replacement = "") Error in gsub(pattern = firm.pat, x = bio$comment, replacement = "") : assertion 'tree->num_tags == num_tags' failed in

More than 9 backreferences in gsub()

喜你入骨 提交于 2019-12-05 15:17:26
问题 How to use gsub with more than 9 backreferences? I would expect the output in the example below to be "e, g, i, j, o". > test <- "abcdefghijklmnop" > gsub("(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)(\\w)", "\\5, \\7, \\9, \\10, \\15", test, perl = TRUE) [1] "e, g, i, a0, a5" 回答1: See Regular Expressions with The R Language: You can use the backreferences \1 through \9 in the replacement text to reinsert text matched by a capturing group. There is no

Delete last two characters in string if they match criteria

断了今生、忘了曾经 提交于 2019-12-05 14:25:01
I have 2 million names in a database. For example: df <- data.frame(names=c("A ADAM", "S BEAN", "A APPLE A", "A SCHWARZENEGGER")) > df names 1 A ADAM 2 S BEAN 3 A APPLE A 4 A SCHWARZENEGGER I want to delete ' A' (white space A) if these are the last two characters of the string. I know that regex is our friend here. How do I efficiently apply a regex function to the last two characters of the string? Desired output: > output names 1 A ADAM 2 S BEAN 3 A APPLE 4 A SCHWARZENEGGER If you want good performance for millions of records, the stringi package is what you need. It even outperforms the

\"invalid regular expression…reason 'Trailing backslash''' error with gsub in R

若如初见. 提交于 2019-12-05 10:06:23
I am getting error message while replacing text in R. x [1] "Easy bruising and bleeding.\\" gsub(as.character(x), "\\", "") Error in gsub(as.character(x), "\\", "") : invalid regular expression 'Easy bruising and bleeding.\', reason 'Trailing backslash' The arguments are in the wrong order. Study help("gsub") . gsub( "\\", "", "Easy bruising and bleeding.\\", fixed=TRUE) #[1] "Easy bruising and bleeding." tl;dr: You need 4 \ s (i.e. \\\\ ) in the first argument of gsub in order to find one literal \ in the third argument of gsub . The overall process is: gsub receives \\\\ , passes \\ regex