I need to remove a single closed parentheses from a string to fix an edge case in a simpler regex problem.
I need to remove text from within parentheses, but the solution I am currently using doesn't handle an extra single closed parentheses well. Should I use a different approach or can I add an extra step to handle this case?
Below is an example where all answers should be brother & I highlighted the line that it fails on below
cleaner = function(x){
x = tolower(x)
## if terms are in brackets - assume this is an alternative and remove
x = stringr::str_remove_all(x, "\\(.*\\)")
## if terms are seperated by semi-colons or commas, take the first, assume others are alternatives and remove
x = gsub("^(.*?)(,|;).*", "\\1", x)
## remove whitespace
x = stringi::stri_replace_all_charclass(x, "\\p{WHITE_SPACE}", "")
x
}
cleaner("brother(bro)")
cleaner("brother;bro")
cleaner("bro ther")
cleaner("(bro)brother ;bro")
cleaner("(bro)brother ;bro)") ## this fails
cleaner("(bro)brother ;(bro") # this doesnt
stringr::str_remove_all("(bro)brother ;bro)", "\\(.*\\)")
Thanks,
Sam