Re:Regex character "$" doesn't mean "end-of-string"
by denzquix on Wednesday March 20, 2024 @11:23AM
(#39767901)
> In Lua it's only the start/end of the string
There's an additional caveat: if you use the optional "init" parameter to specify an offset into the string to start matching, the ^ anchor will match at that offset, which may or may not be what you expect.
Re:Regex character "$" doesn't mean "end-of-string"
by vitiral on Wednesday March 20, 2024 @12:56PM
(#39769193)
That is a good point, and something I've actually (personally) used quite a bit when writing parsers
Re:Regex character "$" doesn't mean "end-of-string"
by Dylan16807 on Friday March 22, 2024 @02:42AM
(#39787973)
> which may or may not be what you expect
I find it hard to imagine any other expectation passing the rubber duck test.
"Oh, so you expected the match to always fail, no matter what the string was?"
Re:Regex character "$" doesn't mean "end-of-string"
by denzquix on Friday March 22, 2024 @07:40AM
(#39789659)
Well, it's not a completely outlandish scenario that the value of `init` might come from a variable that is sometimes at the start of the string and sometimes not, and a newcomer might expect `^` to only match when it is.
Don't get me wrong, it's certainly far more useful as it is, I'm glad it works this way.
> A pattern is a sequence of pattern items. A caret '^' at the beginning of a pattern anchors the match at the beginning of the subject string. A '$' at the end of a pattern anchors the match at the end of the subject string. At other positions, '^' and '$' have no special meaning and represent themselves.
https://www.lua.org/manual/5.3/manual.html#6.4.1
Lua's pattern matching is much simpler than regexes though.
> Unlike several other scripting languages, Lua does not use POSIX regular expressions (regexp) for pattern matching. The main reason for this is size: A typical implementation of POSIX regexp takes more than 4,000 lines of code. This is bigger than all Lua standard libraries together. In comparison, the implementation of pattern matching in Lua has less than 500 lines.
https://www.lua.org/pil/20.1.html