简体   繁体   English

如果下划线在位置 3 之后,则 R 正则表达式删除下划线之后的所有内容

[英]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!谢谢!

Brief简短的

Not sure about length of strings, so I'll assume any length can be used.不确定字符串的长度,所以我假设可以使用任何长度。


Code代码

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

Results结果

Input输入

f__dfty
fd_fgtekg
f_glgkt_s2
f_glgkt_s3
fthssfy_s2
fthssfy_s3
h__gkdnt_s2
sedfgrtsd
aaaaaaa_aaaa

Output输出

f__dfty
fd_fgtekg
f_glgkt
f_glgkt
fthssfy
fthssfy
h__gkdnt
sedfgrtsd
aaaaaaa

Explanation解释

  • Assert position at beginning of line ^在行首断言位置^
  • Capture the following捕获以下内容
    • Optional match of any character 3 times (?:.{3})?任意字符的可选匹配 3 次(?:.{3})?
    • Match between 1 and unlimited of any character not present in the set _\\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]+
  • Match any character any number of times .*匹配任意字符任意次数.*
  • Assert position at the end of the line $断言行尾位置$
  • Replace with first capture group $1替换为第一个捕获组$1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM