[英]R Regex remove everything after Underscore if underscore is after position 3
I have been searching for a solution for two days.我这两天一直在寻找解决方案。
Here is a sample of what my data looks like and what I would like to achieve:以下是我的数据是什么样的以及我想要实现的目标的示例:
dat <- c("f__dfty","fd_fgtekg","f_glgkt_s2","f_glgkt_s3","fthssfy_s2","fthssfy_s3","h__gkdnt_s2","sedfgrtsd")
dat <- c("f__dfty","fd_fgtekg","f_glgkt","f_glgkt","fthssfy","fthssfy","h__gkdnt","sedfgrtsd")
I need to remove everything after an "_", but not if the underscore is in position 2 and or 3 of the string.我需要删除“_”之后的所有内容,但如果下划线位于字符串的第 2 或第 3 位,则不需要。 Not every string will have an underscore.并非每个字符串都有下划线。
Thanks!谢谢!
Not sure about length of strings, so I'll assume any length can be used.不确定字符串的长度,所以我假设可以使用任何长度。
See this code in use here请参阅此处使用的此代码
Regex正则表达式
^((?:.{3})?[^_\s]+).*$
Note: You can actually use ^((?:.{3})?[^_]+).*$
instead, but since my example on regex101 uses multiline input to simplify things, I posted the code I used there.注意:您实际上可以使用^((?:.{3})?[^_]+).*$
代替,但是由于我在 regex101 上的示例使用多行输入来简化事情,因此我发布了我在那里使用的代码。
Substitution代换
$1
f__dfty
fd_fgtekg
f_glgkt_s2
f_glgkt_s3
fthssfy_s2
fthssfy_s3
h__gkdnt_s2
sedfgrtsd
aaaaaaa_aaaa
f__dfty
fd_fgtekg
f_glgkt
f_glgkt
fthssfy
fthssfy
h__gkdnt
sedfgrtsd
aaaaaaa
^
在行首断言位置^
(?:.{3})?
任意字符的可选匹配 3 次(?:.{3})?
_\\s
( \\s
to prevent newline matches in example on regex101; this can be removed from your code if looping through an array/list/etc.) [^_\\s]+
匹配集合_\\s
不存在的任何字符的 1 和无限制( \\s
以防止在 regex101 上的示例中出现换行匹配;如果循环遍历数组/列表/等,这可以从您的代码中删除) [^_\\s]+
.*
匹配任意字符任意次数.*
$
断言行尾位置$
$1
替换为第一个捕获组$1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.