nitely / nim-regex

Pure Nim regex engine. Guarantees linear time matching
https://nitely.github.io/nim-regex/
MIT License
228 stars 21 forks source link

Unexpected replacements for "^", "$", "" #29

Closed kaushalmodi closed 4 years ago

kaushalmodi commented 6 years ago

See https://github.com/nim-lang/Nim/issues/9306.

import regex
echo replace("bar", re"^", "foo")
echo replace("foo", re"$", "bar")
echo replace("foo", re"", "-")

produces:

foobfooafoor # expected: foobar
fobarbarbarfoo # expected: foobar
-f-o-o # expected: -f-o-o-
timotheecour commented 6 years ago

as mentioned in https://github.com/nim-lang/Nim/issues/9306#issue-369016855 what nre does here seems correct, and what nim-regex should do

Using nre:

import nre
echo replace("bar", re"^", "foo")
echo replace("foo", re"$", "bar")
echo replace("foo", re"", "-")
produces:

foobar
foobar
-f-o-o-
nitely commented 6 years ago

I think this a duplicate of https://github.com/nitely/nim-regex/issues/13. I'll try to fix it (again) this weekend soon. IIRC, it's not as easy as implementing the nre version of this function, there is some issue in how empty matches are handled.

kaushalmodi commented 4 years ago

Thank you!!