Discard the last character, except those that precede certain characters

R has been using it for a while, but my regular expression skills are a beginner. I am trying to remove "s" if its last character is in EXCEPT, if it is preceded by "i" or "s", for example. Example...

dfx <- c("class","guests","trips","sassy","basis","fruits")
dfx <- sub("s$","",dfx)
View(dfx)

Any examples or recommendations would be appreciated.

+4
source share
2 answers

You can use a twist for this. I am not the best of them, but check out this tutorial to learn more. For example, (?<!s)swill match "s", which is not preceded by "s". Please note that you must set perl=Tthem to work in R.

sub("(?<!s|i)s$","",dfx,perl = T)
# [1] "class" "guest" "trip"  "sassy" "basis" "fruit"
+6
source

s(?<![is]s)\b - , .

https://regex101.com/r/ANyYB0/1

,

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

Source: https://habr.com/ru/post/1696361/


All Articles