(ends-with) regex issue

Started by pjot, December 19, 2007, 11:31:17 AM

Previous topic - Next topic

pjot

Hi,



The other day I was stumbling into this problem (and newdep: yes I've read the manual now :-) )


Quote
newLISP v.9.2.10 on Linux, execute 'newlisp -h' for more info.



> (set 'S "abc.def.ghi")

"abc.def.ghi"

> (ends-with S "ghi" 1)

true

> (ends-with S "def|ghi" 1)

nil

>


As is visible from above, I try to find out if a string "ends with" a 'def' or a 'ghi'. I would expect true as a result, but it seems not to be the case.



This however is working:


Quote
> (ends-with S "def$|ghi$" 1)

true


But isn't the dollarsign a little bit superfluous here? I mean, what is the meaning of 'ends-with'...?



Peter

Cyril

#1
Quote from: "pjot"But isn't the dollarsign a little bit superfluous here? I mean, what is the meaning of 'ends-with'...?


Things are much more amusing! I have made a little exploration: if second argument regexp matches the first argument string and the first match found is not at the end of line, then function returns 'nil'. Lutz, it seems like a bug for me! (I believe starts-with is not affected)


> (ends-with "abcdef" "abc|def" 1)
nil
> (ends-with "abcdef" "def|abc" 1)
true
> (ends-with "abcdef" "qed|def" 1)
true
With newLISP you can grow your lists from the right side!

pjot

#2
You are right, and if it is not a bug, at least it is inconcistent:


Quote
> (starts-with "12345" "1|2" 1)

true

> (starts-with "12345" "2|1" 1)

true

> (ends-with "12345" "5|4" 1)

true

> (ends-with "12345" "4|5" 1)

nil




So indeed (starts-with) is working correctly.



Peter

Lutz

#3
Internally I add a $ at the end of the pattern, but this doesn't work with alternate patterns. What I will do is parenthesise the expression and add the $ sign: (pattern)$ this hopefully works for all cases.



As a workaround parenthesize all patterns:




(ends-with "abcdef" "(abc|def)" 1) => true

starts-with is always fine because it works different, checking the position of the succeeeding regex to be 0.



Lutz