Been using R for a while but my regex skills are novice grade. I'm attempting to remove the "s" if its the final character in a word EXCEPT if its preceded by an "i" or an "s" for example. Sample...
一直使用R,但我的正则表达技能是新手等级。我试图删除“s”,如果它是单词中的最后一个字符,除非前面有“i”或“s”。样品...
dfx <- c("class","guests","trips","sassy","basis","fruits")
dfx <- sub("s$","",dfx)
View(dfx)
Any examples or guidance would be appreciated.
任何例子或指导将不胜感激。
2 个解决方案
#1
6
You can use lookarounds for this. I'm not the best at them, but check out this tutorial to learn more. For example (?<!s)s
will match an "s" that is not preceded by an "s". Note you have to set perl=T
for these to work in R.
你可以使用lookarounds。我不是最好的,但请查看本教程以了解更多信息。例如(?
sub("(?<!s|i)s$","",dfx,perl = T)
# [1] "class" "guest" "trip" "sassy" "basis" "fruit"
#2
1
s(?<![is]s)\b
is what's needed.
s(?
https://regex101.com/r/ANyYB0/1
Benchmark showing the right way and the wrong way
基准显示正确的方式和错误的方式
Regex1: s(?<![is]s)\b
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 3
Elapsed Time: 0.30 s, 295.43 ms, 295426 µs
Matches per sec: 507,741
Regex2: (?<![is])s\b
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 3
Elapsed Time: 0.72 s, 718.22 ms, 718215 µs
Matches per sec: 208,851
#1
6
You can use lookarounds for this. I'm not the best at them, but check out this tutorial to learn more. For example (?<!s)s
will match an "s" that is not preceded by an "s". Note you have to set perl=T
for these to work in R.
你可以使用lookarounds。我不是最好的,但请查看本教程以了解更多信息。例如(?
sub("(?<!s|i)s$","",dfx,perl = T)
# [1] "class" "guest" "trip" "sassy" "basis" "fruit"
#2
1
s(?<![is]s)\b
is what's needed.
s(?
https://regex101.com/r/ANyYB0/1
Benchmark showing the right way and the wrong way
基准显示正确的方式和错误的方式
Regex1: s(?<![is]s)\b
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 3
Elapsed Time: 0.30 s, 295.43 ms, 295426 µs
Matches per sec: 507,741
Regex2: (?<![is])s\b
Completed iterations: 50 / 50 ( x 1000 )
Matches found per iteration: 3
Elapsed Time: 0.72 s, 718.22 ms, 718215 µs
Matches per sec: 208,851