Remove last character from string if vowel

Hello, I have a corpus named 'test', filled with character strings, and I'd like to remove the last character from each string ONLY if it is a vowel. The current code I'm working with is this:

test2 <- tm_map(test, content_transformer(function(x) gsub(x, pattern = "[aeyuio]+$", replacement = "")))

It doesn't seem to be doing it

Any ideas what I could change my code to to get it to work?

It's easier with stringer

library(stringr)
look_for <- "[aeioy]$"
replace_with <- ""
textone <- "No hay morors en la costa"
texttwo <- "The coast is clear."
str_replace(textone, look_for, replace_with)
#> [1] "No hay morors en la cost"
str_replace(texttwo, look_for, replace_with)
#> [1] "The coast is clear."

(The purpose of defining the search and replace arguments is re-using the code as a snippet.)
Created on 2020-04-09 by the reprex package (v0.3.0)

1 Like

I ended up having to do this

test <- tm_map(test, content_transformer(function(x) gsub(x, pattern = "[aeiou]+\\s", replacement = " ")))
test <- tm_map(test, content_transformer(function(x) gsub(x, pattern = "[aeiou]+$", replacement = " ")))

One for where there was a space after, another for when it was the last word of the line

1 Like

The absolutely hardest part of regex is getting the search pattern right!

Great. Please mark your post as the solution for the benefit of those to follow.

Oh, and by the way, ? is zero or more

1 Like

Thanks for your swift response :slight_smile:

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.