我想用正则表达式删除重复的单词
let url = "/courses?category=programming&filter=free&filter=free"
const az = url.replace(/(\b\S.+\b)(?=.*\1)/g, "").trim();
console.log(az) // /courses?category=programmingfree&filter=free
Run Code Online (Sandbox Code Playgroud)
我想要得到/courses?category=programming&filter=free
为了显示我运行的回归结果,我有一个包含估计值和相应置信区间的小标题:
\nlibrary(tidyverse)\nlibrary(magrittr\n\nmydata <- structure(list(term = structure(c(1L, 3L, 4L), .Label = c("Intercept", \n"Follow-up time (years)", "Age (years)", "Sex (male)", "Never smoker (reference)", \n"Current smoker", "Former smoker", "Obesity (=30 kg/m\xc2\xb2)", "BMI (kg/m\xc2\xb2)", \n"Diabetes", "Glucose (mmol/L)", "Glucose lowering medication use", \n"Hypertension", "Systolic blood pressure (mmHg)", "Diastolic blood pressure (mmHg)", \n"Antihypertensive medication use", "Hypercholesterolemia", "LDL cholesterol (mmol/L)", \n"Lipid lowering medication use", "Chronic kidney disease (mL/min/1.73m\xc2\xb2)", \n"=90 (reference)", "60-89", "=60"), class = c("ordered", "factor"\n)), estimate = c(518.38, 0.98, 1.07), conf_low = c(178.74, 0.93, \n0.96), …Run Code Online (Sandbox Code Playgroud) 我有一个 .bed (.tsv) 文件,如下所示:
chr1 0 100000
chr1 100000 200000
chr1 200000 300000
chr1 300000 425234
Run Code Online (Sandbox Code Playgroud)
我想使用 sed 或 awk 仅对第 3 列中以“000”结尾的值执行操作 -1,以便输出如下所示:
chr1 0 99999
chr1 100000 199999
chr1 200000 299999
chr1 300000 425234
Run Code Online (Sandbox Code Playgroud)
令人尴尬的是,我想出的最好的办法是:
awk {sub(/000$/,"999",$3); print $1,$2,$3}' oldfile > newfile
Run Code Online (Sandbox Code Playgroud)
它只是用最后 3 位数字代替 999,而不是实际相减。任何帮助总是值得赞赏!
我有一个大型 data.table,包含超过 700 万行和 38 列。其中一列是字符向量,其中包含很长的描述性句子。我知道每个句子的第一个单词是一个类别,第二个单词是一个名称,我需要将这两个单词放入两个新列中以供以后分析。
这可能不能很好地说明时间差异,因为它太小了(实际上system.time()在这个例子中给出了 0),但这里有一个玩具字符串来说明我想要做的事情:
# Load libraries:
library(data.table)
library(stringr)
# Create example character string:
x <- c("spicy apple cream", "mild peach melba", "juicy strawberry tart")
id <- c(1,2,3)
# Create dt:
mydt <- data.table(id = id, desert = x)
Run Code Online (Sandbox Code Playgroud)
假设在我的真实数据中,我想从每个字符串中提取第一个单词,并将其放入一个名为“category”的新变量中,然后从每个字符串中提取第二个单词并将其放入一个名为“fruit_name”的新变量中。
词法上最简单的方法似乎是使用stringr::word()which 很有吸引力,因为它避免了计算复杂的正则表达式的需要:
# Add a new category column:
mydt[, category := stringr::word(desert, 1)]
# Add a new fruit name column:
mydt[, fruit_name := stringr::word(desert, 2)]
Run Code Online (Sandbox Code Playgroud)
虽然这在小数据集上工作得很好,但在我的真实数据集上却花了很长时间(我怀疑它挂起了,尽管我杀死了它并在 10 分钟后重新启动了 R)。就上下文而言,该数据集中的其他字符向量类型操作大约需要 20 秒才能运行,因此该函数似乎特别耗费人力和计算资源。
相反,如果我使用正则表达式, …
我正在使用 python,并且有一串电子邮件地址,如下所示。
email_addr = 'test@domain.com, test1@domain.com, test2@domain.com'
Run Code Online (Sandbox Code Playgroud)
上面的字符串看起来不错,但是有时我收到的数据中包含空白电子邮件地址。
例如
email_addr = ' , , test@domain.com, test1@domain.com, , , ,test2@domain.com
Run Code Online (Sandbox Code Playgroud)
我正在使用str.split(',')并检查很多错误。想知道是否有更好的方法来做到这一点?
我期望的最终值:
email_addr = ' , , test@domain.com, test1@domain.com, , , ,test2@domain.com
Run Code Online (Sandbox Code Playgroud)
到:
email_addr = 'test@domain.com,test1@domain.com,test2@domain.com'
Run Code Online (Sandbox Code Playgroud) 为什么以下代码:
"AB" .match(/(AB)/);
归还这个:
["ab","ab"]
虽然ab在字符串中出现一次ab,为什么ab在数组中出现两次?
如何在 R 中创建一个函数来定位字符串中第一个数字的单词位置?
例如:
string1 <- "Hello I'd like to extract where the first 1010 is in this string"
#desired_output for string1
9
string2 <- "80111 is in this string"
#desired_output for string2
1
string3 <- "extract where the first 97865 is in this string"
#desired_output for string3
5
Run Code Online (Sandbox Code Playgroud) 我知道那里有很多帖子,但我真的找不到任何匹配项。我有一个字符串
"com/i598816441262.htm?sourceType=item&ttid=227200"
我想提取 'i' 和 '.' 之后的数字,在示例中为 598816441262,我可以针对整个内容,“/i598816441262”。但我只想要数字,如果我能做一次,我不想做两次。
求求各位大大给点建议!
我有一个字符串数组,如下所示:
const strings = ['Prepayment', 'Postpayment', 'Complete']
const addDash = (str: string) =>
str.startsWith('Pre') || str.startsWith('Post') ? str.replace(' ', '-') : str;
Run Code Online (Sandbox Code Playgroud)
我想要以下数组:
const result = strings.map(str => addDash(str))
// => ['Pre-payment', 'Post-payment', 'Complete'] // want result to equal this
Run Code Online (Sandbox Code Playgroud)
谁能建议什么正则表达式可以帮助我完成这项任务?
我想检查 url 是否有效,在 lua 中执行此操作的正确正则表达式是什么?我尝试过像这样的正则表达式
string.match('https://stackoverflow.com/', '[a-z]*:\/\/[^ >,;]*')
Run Code Online (Sandbox Code Playgroud)
但出现错误
invalid escape sequence near ''[a-z]*:\/'
Run Code Online (Sandbox Code Playgroud)
更新:
string.match('https://stackoverflow.com/', '[a-z]*://[^ >,;]*')
Run Code Online (Sandbox Code Playgroud)
是正确答案
javascript ×3
r ×3
regex ×3
string ×3
awk ×1
data.table ×1
dplyr ×1
format ×1
locate ×1
lua ×1
lua-patterns ×1
performance ×1
php ×1
python ×1
query-string ×1
sed ×1
stringr ×1
url ×1