gsub

How to remove random excess of slashes from url?

两盒软妹~` 提交于 2020-06-28 04:19:13
问题 How to remove random excess of slashes from url or just validate it? For example, valid statements : http://domain.com/url/url2 https://domain.com/url/url2 www.domain.com/url/url2 invalid statements: http://domain.com//url/url2 https://domain.com/////url/url2 www.domain.com/url/////////url2 Thanks for help! 回答1: Use regular expressions: require 'uri' url = URI.parse('https://domain.com/////url/url2') url.path.gsub! %r{/+}, '/' p url.to_s 回答2: this pattern do the job (with http(s) or not) :

Replace entire expression that contains a specific string

拟墨画扇 提交于 2020-06-08 06:28:05
问题 I have data frame that has a column with large number of file names like: d <- c("harry11_scott80_norm.avi","harry11_norm.avi","harry11_scott80_lpf.avi", "joel51_lpf.avi","rich82_joel51_lpf.avi") I want R to replace all expressions with two people names like harry11_scott80_norm.avi with the expression incongruent and all the ones with single person name like harry11_norm.avi with congruent . I could use gsub to do that: dd <- gsub("harry11_scott80_norm.avi", "incongruent", d) but I got a lot

Search for unicode values in character string

老子叫甜甜 提交于 2020-05-15 04:49:17
问题 I am trying to identify unique unicode values in a data frame composed of character strings. I have tried using the grep function, however I encounter the following error Error: '\U' used without hex digits in character string starting ""\U" A example data frame time sender message 1 2012-12-04 13:40:00 1 Hello handsome! 2 2012-12-04 13:40:08 1 \U0001f618 3 2012-12-04 14:39:24 1 \U0001f603 4 2012-12-04 16:04:25 2 <image omitted> 73 2012-12-05 06:02:17 1 Haha not white and blue... White with

Replace dots using `gsub`

被刻印的时光 ゝ 提交于 2020-05-09 06:30:14
问题 I am trying to replace all the "." in a specific column of my data frame with "/". There are other characters in each cell and I want to make sure I only change the "."'s. When I use gsub, I get an output that appears to make the changes, but then when I go to View(), the changes are not actually made...I thought gsub was supposed to actually change the value in the data frame. Am I using it incorrectly? I have my code below. gsub(".", "/", spy$Identifier, ignore.case = FALSE, perl = FALSE,

How to replace column with strings with look-up codes in R

风格不统一 提交于 2020-04-07 05:25:49
问题 Imagine that I have a dataframe or datatable with strings column where one row looks like this: a1; b: b1, b2, b3; c: c1, c2, c3; d: d1, d2, d3, d4 and a look-up table with codes for mapping each of these strings. For example: string code a1 10 b1 20 b2 30 b3 40 c1 50 c2 60 ... I would like to have a mapping function that maps this string to code: 10; b: 20, 30, 40; c: 50, 60, 70; d: 80, 90, 100 I have a column of these strings in data.table/data.frame (more tha 100k) so any quick solution

in R, use gsub to remove all punctuation except period

喜欢而已 提交于 2020-03-09 09:38:27
问题 I am new to R so I hope you can help me. I want to use gsub to remove all punctuation except for periods and minus signs so I can keep decimal points and negative symbols in my data. Example My data frame z has the following data: [,1] [,2] [1,] "1" "6" [2,] "2@" "7.235" [3,] "3" "8" [4,] "4" "$9" [5,] "£5" "-10" I want to use gsub("[[:punct:]]", "", z) to remove the punctuation. Current output > gsub("[[:punct:]]", "", z) [,1] [,2] [1,] "1" "6" [2,] "2" "7235" [3,] "3" "8" [4,] "4" "9" [5,]

Extract date from a given string in R

醉酒当歌 提交于 2020-02-25 05:03:47
问题 Here is a string that I have "7MA_S_VE_MS_FB_MEASURE_P1_2013-08-21_17-42-19.BMP" I am trying to extract dates this way: library(stringr) as.Date(str_extract(test,"[0-9]{4}/[0-9]{2}/[0-9]{2}"),"%Y-%m-%d") I am getting NA for this. Desired output is 2013-08-21 Can someone point me in the right direction? 回答1: You have replaced your dash - with a slash / in your regular expression. as.Date(str_extract(string, "[0-9]{4}-[0-9]{2}-[0-9]{2}"), format="%Y-%m-%d") # [1] "2013-08-21" But you can also

keep only alphanumeric characters and space in a string using gsub

烈酒焚心 提交于 2020-02-23 09:15:42
问题 I have a string which has alphanumeric characters, special characters and non UTF-8 characters. I want to strip the special and non utf-8 characters. Here's what I've tried: gsub('[^0-9a-z\\s]','',"�+ Sample string here =�{�>E�BH�P<]�{�>") However, This removes the special characters (punctuations + non utf8) but the output has no spaces. gsub('/[^0-9a-z\\s]/i','',"�+ Sample string here =�{�>E�BH�P<]�{�>") The result has spaces but there are still non utf8

Matching position in gsub or scan

痴心易碎 提交于 2020-01-23 09:48:33
问题 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 ? 回答1: "hello".gsub(/./) { Regexp.last_match.offset(0).first } => "01234" See Regexp.last_match and MatchData. 回答2: 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?..

Matching position in gsub or scan

痞子三分冷 提交于 2020-01-23 09:47:27
问题 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 ? 回答1: "hello".gsub(/./) { Regexp.last_match.offset(0).first } => "01234" See Regexp.last_match and MatchData. 回答2: 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?..