I use regex-applicative library.
I want to remove suffix ".end" but only if it's really a suffix (a tail of a string, not somewhere in the middle of the string) and to use replace for it.
replace (some anySym <* ".end") "start.end"
It works: "start.end" -> "start"
But it does not work with "start.end.extra" -> "start.extra" (I expect "start.end.extra" result, because ".end" is not a suffix more - it's in the middle).
I tried:
replace (some anySym <* ".end" *> empty) "start.end"
replace (empty <* some anySym <* ".end" *> empty) "start.end"
and a lot of other combinations, but it does not work as I expect. Is it possible to remove a suffix with this library?
replace
is for finding all the places in a string where the RE matches, and replacing them with something else. You don't want that, so don't use replace
. You have a regex defining how you want the whole string to be shaped, so match
on that regex to get the information from the match:
stripEndSuffix :: String -> String
stripEndSuffix =
let findSuffix = match $ some anySym <* string ".end"
in fromMaybe <*> findSuffix