gsub

Lua gsub - How to set max character limit in regex pattern

倖福魔咒の 提交于 2019-12-31 03:23:20
问题 From strings that are similar to this string: |cff00ccffkey:|r value I need to remove |cff00ccff and |r to get: key: value The problem is that |cff00ccff is a color code. I know it always starts with |c but the next 8 characters could be anything. So I need a gsub pattern to get the next 8 characters (alpha-numeric only) after |c . How can I do this in Lua? I have tried: local newString = string.gsub("|cff00ccffkey:|r value", "|c%w*", "") newString = string.gsub(newString, "|r", "") but that

r code removing words containing @

守給你的承諾、 提交于 2019-12-31 02:15:27
问题 I want to replace all words containing the symbol @ with a specific word. I am used gsub and therefore am applying it to a character vector. The issue that keeps occuring is that when I use: gsub(".*@.*", "email", data) all of the text in that portion of the character vector gets deleted. There are multiple different emails all with different lengths so I can't set the characters prior and characters after to a specific number. Any suggestions? I've done my fair share of reading about regex

ruby regexp to replace equations

二次信任 提交于 2019-12-30 19:03:06
问题 I have a some HTML text in mathjax format: text = "an inline \\( f(x) = \frac{a}{b} \\) equation, a display equation \\[ F = m a \\] \n and another inline \\(y = x\\)" (Note: equations are delimited by single slashes, e.g. \( , not \\( , the extra \ is just escaping the first one for ruby text). I want to get the output that substitutes this into, say an image created by latex.codecogs, e.g. desired_output = "an inline <img src="http://latex.codecogs.com/png.latex?f(x) = \frac{a}{b}\inline"/>

check capital words in text and extract it

百般思念 提交于 2019-12-30 09:58:33
问题 I want to extract all capital words from the text. Lets say my data is like--> Text<-c('I am JAY','I AM NOT HAPPY','YOU ARE IRRITATING','so Funny','hEY) So output should be like --> > output [1] "I JAY" "I AM NOT HAPPY" "YOU ARE IRRITATING" "" "" Please help me for this. 回答1: Another option is library(stringr) sapply(str_extract_all(Text, '\\b[A-Z]+\\b'), paste, collapse=' ') # [1] "I JAY" "I AM NOT HAPPY" "YOU ARE IRRITATING" #[4] "" "" Or gsub("[a-z][A-Za-z]+|[A-Za-z][a-z]+", '', Text) #[1]

Modifying a character in a string in Lua

大憨熊 提交于 2019-12-30 06:37:28
问题 Is there any way to replace a character at position N in a string in Lua. This is what I've come up with so far: function replace_char(pos, str, r) return str:sub(pos, pos - 1) .. r .. str:sub(pos + 1, str:len()) end str = replace_char(2, "aaaaaa", "X") print(str) I can't use gsub either as that would replace every capture, not just the capture at position N. 回答1: Strings in Lua are immutable. That means, that any solution that replaces text in a string must end up constructing a new string

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

可紊 提交于 2019-12-29 07:14:30
问题 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! 回答1: string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs = subs.first.map(&:to_a) 1

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

允我心安 提交于 2019-12-29 07:14:16
问题 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! 回答1: string = "this is a test" subs = ['a'=>'@','i'=>'!','s'=>'$'] subs = subs.first.map(&:to_a) 1

Removing leading zeros from alphanumeric characters in R

拜拜、爱过 提交于 2019-12-29 06:44:09
问题 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

How to backreference in Ruby regular expression (regex) with gsub when I use grouping?

你说的曾经没有我的故事 提交于 2019-12-29 03:53:06
问题 I would like to patch some text data extracted from web pages. sample: t="First sentence. Second sentence.Third sentence." There is no space after the point at the end of the second sentence. This sign me that the 3rd sentence was in a separate line (after a br tag) in the original document. I want to use this regexp to insert "\n" character into the proper places and patch my text. My regex: t2=t.gsub(/([.\!?])([A-Z1-9])/,$1+"\n"+$2) But unfortunately it doesn't work: "NoMethodError:

How to get coefficients of polynomial expression

淺唱寂寞╮ 提交于 2019-12-25 09:13:03
问题 I used rSymPy and obtained following expression: "1 - 0.7*B - 0.3*B**2" Now I want to extract the coefficients of B and Coefficients of B^2, and stored in a matrix. I tried gsub function in R, any suggestions? 回答1: Do you mean like: > x <- "1 - 0.7*B - 0.3*B**2" > m <- gregexpr("[0-9]+\\.[0-9]+",x) > out <- unlist(regmatches(x,m) ) > out [1] "0.7" "0.3" A more complex example: > x <- c("1 - 0.7*B - 0.3*B**2", "1 - 0.3*B - 0.7*B**2","1 - 1.3*B - 0.6*B**2") > m <- gregexpr("[0-9]+\\.[0-9]+",x)