gsub

Remove punctuation but keeping emoticons?

不羁岁月 提交于 2019-11-29 18:28:43
问题 Is that possible to remove all the punctuations but keeping the emoticons such as :-( :) :D :p structure(list(text = structure(c(4L, 6L, 1L, 2L, 5L, 3L), .Label = c("ãããæããããéãããæãããInappropriate announce:-(", "@AirAsia your direct debit (Maybank) payment gateways is not working. Is it something you are working to fix?", "@AirAsia Apart from the slight delay and shortage of food on our way back from Phuket, both flights were very smooth. Kudos :)", "RT @AirAsia: ØØÙØÙÙÙÙ ÙØØØ ØØØÙ ÙØØØØÙ

Removing multiple spaces and trailing spaces using gsub

我只是一个虾纸丫 提交于 2019-11-29 14:49:37
问题 How can I remove multiple spaces and trailing spaces using only 1 gsub? I already made this function trim <- function(x) gsub(' {2,}',' ',gsub('^ *| *$','',x)) , but i'm trying to rewrite it with only 1 gsub. Actually, I want lean how to match something based in what is after/before it with gsub. In this example I need to match all spaces that are preceeded by a single space, and replace them by '' 回答1: Use a positive lookbehind to see if the current space is preceded by a space: ^ *|(?<= ) |

Convert numbers to letters

泪湿孤枕 提交于 2019-11-29 13:45:24
I have the following vector: x <- c(11, 12, 21, 22) And I want to convert it to the corresponding letters, i.e., I want to get this result: AA AB BA BB How do I make this? I bet there's a simple answer and that it goes through using the reserved LETTERS vector, but I can't figure out a solution. This is the best I've managed to come up with so far (you might want to take the kids out of the room): > paste0(gsub(1, LETTERS[1], substr(x, 1, 1)), gsub(2, LETTERS[2], substr(x, 1, 1))) [1] "A1" "A1" "2B" "2B" Since this just involves one-to-one character substitution, it might be simplest to just

Ruby regex gsub a line in a text file

廉价感情. 提交于 2019-11-29 12:02:45
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 line argument. So I got string = IO.read(ARGV[0]) string = string.gsub(/^(test)$/,'X\1X') puts string It outputs

Ruby regex- does gsub store what it matches?

三世轮回 提交于 2019-11-29 11:29:23
问题 If i use .gsub(/matchthisregex/,"replace_with_this") does gsub store what it matches with the regex somewhere? I'd like to use what it matches in my replacement string. For example something like "replace_with_" + matchedregexstring + "this" in my above example where the matchedregexstring would be the stored match from gsub? Sorry if that was confusing, I don't know how else to word that. 回答1: From the fine manual: If replacement is a String it will be substituted for the matched text. It

Use regex to insert space between collapsed words

天涯浪子 提交于 2019-11-29 09:30:10
问题 I'm working on a choropleth in R and need to be able to match state names with match.map(). The dataset I'm using sticks multi-word names together, like NorthDakota and DistrictOfColumbia. How can I use regular expressions to insert a space between lower-upper letter sequences? I've successfully added a space but haven't been able to preserve the letters that indicate where the space goes. places = c("NorthDakota", "DistrictOfColumbia") gsub("[[:lower:]][[:upper:]]", " ", places) [1] "Nort

All possible combinations of selected character substitution in a string in ruby

跟風遠走 提交于 2019-11-29 08:03:21
I was wondering if there is a simple way to do every combination of selected character substitutions in ruby in a simple way. An example: string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs.combination.each { |c| string.gsub c } would yield "this is @ test" "th!s !s a test" "thi$ i$ a te$t" "th!s !s @ test" "thi$ i$ @ te$t" "th!$ !$ a te$t" "th!$ !$ @ te$t" Thanks for the help! string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs = subs.first.map(&:to_a) 1.upto(subs.length).each do |n| subs.combination(n).each do |a| p a.each_with_object(string.dup){|pair, s| s

Lua string.gsub with a hyphen

做~自己de王妃 提交于 2019-11-29 07:16:14
I have two strings - each string has many lines like the following: value_1 = "DEFAULT-VLAN" value_2 = "WAN" data = "HOSTNAME = DEFAULT-VLAN" result = string.gsub(data,value_1,value_2) print(result) Result: data = "HOSTNAME = DEFAULT-VLAN" When the hyphen ("-") is deleted from the value it is working. Is there an easy way to solve this? Thanks! - is a magic character in Lua patterns. You need to escape it. Change value_1 = "DEFAULT-VLAN" to: value_1 = "DEFAULT%-VLAN" This is because string.gsub takes a pattern similar to Regex—it does not do a "literal" replacement; this means you need to

Making gsub only replace entire words?

ぃ、小莉子 提交于 2019-11-29 06:24:48
(I'm using R.) For a list of words that's called "goodwords.corpus", I am looping through the documents in a corpus, and replacing each of the words on the list "goodwords.corpus" with the word + a number. So for example if the word "good" is on the list, and "goodnight" is NOT on the list, then this document: I am having a good time goodnight would turn into: I am having a good 1234 time goodnight **I'm using this code (EDIT- made this reproducible): goodwords.corpus <- c("good") test <- "I am having a good time goodnight" for (i in 1:length(goodwords.corpus)){ test <-gsub(goodwords.corpus[[i

Removing leading zeros from alphanumeric characters in R

别来无恙 提交于 2019-11-29 06:03:52
I have a character vector d with alphanumeric characters d <- c("012309 template", "separate 00340", "00045", "890 098", "3405 garage", "matter00908") d [1] "012309 template" "separate 00340" "00045" "890 098" "3405 garage" "matter00908" How can I remove the leading zeros from all the numbers in R? as.numeric will remove all leading zeros only in numeric or integer vectors. I have tried gsub with regex but could not get the desired results. The expected output is as follows out <- c("12309 template", "seperate 340", "45", "890 98", "3405 garage", "matter908") out [1] "12309 template" "seperate