1

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?

RandomB
  • 3,367
  • 19
  • 30
  • Have you tried `\.end$`? – markalex Apr 05 '23 at 18:52
  • @markalex it does not parse a string for special regex-syntax, so "$" is not a special symbol for it (`$` is literally matching symbol) – RandomB Apr 05 '23 at 19:04
  • "End of input" is not a concept that library treats as part of its supported language space. It's just a design choice. If it's what you need, this isn't the library for you. – Carl Apr 05 '23 at 19:41
  • @Carl It's not available as a combinator, but `match` and the like let you require it at a whole-regex level. – amalloy Apr 06 '23 at 19:30

1 Answers1

2

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
amalloy
  • 89,153
  • 8
  • 140
  • 205