gsub

How to use ruby gsub Regexp with many matches?

和自甴很熟 提交于 2019-12-05 09:53:36
问题 I have csv file contents having double quotes inside quoted text test,first,line,"you are a "kind" man",thanks again,second,li,"my "boss" is you",good I need to replace every double quote not preceded or succeeded by a comma by "" test,first,line,"you are a ""kind"" man",thanks again,second,li,"my ""boss"" is you",good so " is replaced by "" I tried x.gsub(/([^,])"([^,])/, "#{$1}\"\"#{$2}") but didn't work 回答1: Your regex needs to be a little more bold, in case the quotes occur at the start

Meaning of regular expressions like - \\d , \\D, ^ , $ etc [duplicate]

删除回忆录丶 提交于 2019-12-05 03:12:24
问题 This question already has an answer here : Reference - What does this regex mean? (1 answer) Closed 3 years ago . What do these expressions mean? Where can I learn about their usage? \\d \\D \\s \\S \\w \\W \\t \\n ^ $ \ | etc.. I need to use the stringr package and i have absolutely no idea how to use these . 回答1: From ?regexp , in the Extended Regular Expressions section: The caret ‘^’ and the dollar sign ‘$’ are metacharacters that respectively match the empty string at the beginning and

Escape spaces in a linux pathname with Ruby gsub

亡梦爱人 提交于 2019-12-05 00:10:30
I am trying to escape the spaces in a Linux path. However, whenever I try to escape my backslash I end up with a double slash. Example path: /mnt/drive/site/usa/1201 East/1201 East Invoice.pdf So that I can use this in Linux I want to escape it as: /mnt/drive/site/usa/1201\ East/1201\ East\ Invoice.pdf So I'm trying this: backup_item.gsub("\s", "\\\s") But I get an unexpected output of /mnt/drive/site/usa/1201\\ East/1201\\ East\\ Invoice.pdf Stefan is right; I just want to point out that if you have to escape strings for shell use you should check Shellwords::shellescape : require 'shellwords

removing trailing spaces with gsub in R [duplicate]

强颜欢笑 提交于 2019-12-04 21:32:56
问题 This question already has answers here : How to trim leading and trailing whitespace? (13 answers) Closed 3 years ago . Does anyone have a trick to remove trailing spaces on variables with gsub? Below is a sample of my data. As you can see, I have both trailing spaces and spaces embedded in the variable. county <- c("mississippi ","mississippi canyon","missoula ", "mitchell ","mobile ", "mobile bay") I can use the following logic to remove all spaces, but what I really want is to only move

R regex gsub separate letters and numbers

丶灬走出姿态 提交于 2019-12-04 21:16:21
问题 I have a string that's mixed letters and numbers: "The sample is 22mg" I'd like to split strings where a number is immediately followed by letter like this: "The sample is 22 mg" I've tried this: gsub('[0-9]+[[aA-zZ]]', '[0-9]+ [[aA-zZ]]', 'This is a test 22mg') but am not getting the desired results. Any suggestions? 回答1: You need to use capturing parentheses in the regular expression and group references in the replacement. For example: gsub('([0-9])([[:alpha:]])', '\\1 \\2', 'This is a

Ruby gsub function

你离开我真会死。 提交于 2019-12-04 19:42:29
I'm trying to create a BBcode [code] tag for my rails forum, and I have a problem with the expression: param_string.gsub!( /\[code\](.*?)\[\/code\]/im, '<pre>\1</pre>' ) How do I get what the regex match returns (the text inbetween the [code][/code] tags), and escape all the html and some other characters in it? I've tried this: param_string.gsub!( /\[code\](.*?)\[\/code\]/im, '<pre>' + my_escape_function('\1') + '</pre>' ) but it didn't work. It just passes "\1" as a string to the function. You should take care of the greedy behavior of the regular expressions. So the correct code looks like

Using more than nine back references in an R regex

女生的网名这么多〃 提交于 2019-12-04 15:07:59
The code below does not work, because the replacement string for \10, \11, and so on, cannot be read properly. It reads \10 as \1 and print 0 instead, can you help me fix it? There is an answer in one of the threads, saying that I am supposed to use capturing or naming groups, but I don't really understand how to use them. headline <- gsub("regexp with 10 () brackets", "\\1 ### \\2 ### \\3 ### \\4 ### \\5 ### \\6 ### \\7 ### \\8 ### \\9 ### \\10### \\11### \\12### \\13### \\14### \\15### \\16", page[headline.index]) According to ?regexp , named capture has been available in regexpr() and

Removing multiple commas and trailing commas using gsub

折月煮酒 提交于 2019-12-04 14:44:25
问题 This question is very similar to Removing multiple spaces and trailing spaces using gsub, except that I'd like to apply it to commas instead of spaces. For example, I'd like a function TrimCommas to turn x into y : x <- c("a,b,c", ",a,b,,c", ",,,a,,,b,c,,,") # y <- TrimCommas(x) # presumably y <- c("a,b,c", "a,b,c", "a,b,c") The solution for spaces was gsub("^ *|(?<= ) | *$", "", x, perl=T) , so I'm hoping comparing the solution for this will help explain some regex fundamentals as well. 回答1:

using negative conditions within regular expressions

删除回忆录丶 提交于 2019-12-04 12:18:32
问题 Is it possible to use negative matches within gsub expressions? I want to replace strings starting by hello except those starting by hello Peter my-string.gsub(/^hello@/i, '') What should I put instead of the @ ? 回答1: Sounds like you want a negative lookahead: >> "hello foo".gsub(/hello (?!peter)/, 'lala ') #=> "lala foo" >> "hello peter".gsub(/hello (?!peter)/, 'lala ') #=> "hello peter" 回答2: As Michael told you you need a negative lookahead. For your example is something like: my_string

Split strings at the first colon

China☆狼群 提交于 2019-12-04 09:37:32
问题 I am reading data files in text format using readLines . The first 'column' is complicated text that I do not need. The next columns contain data that I do need. The first 'column' and the data are separated by a colon (:). I wish to split each row at the first colon and delete the resulting text string, keeping only the data. Below is an example data file. One potential complication is that one line of data contains multiple colons. That line may at some point become my header. So, I