(ends-with) regex issue

For the Compleat Fan
Locked
pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

(ends-with) regex issue

Post by pjot »

Hi,

The other day I was stumbling into this problem (and newdep: yes I've read the manual now :-) )
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:
> (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
Posts: 183
Joined: Tue Oct 30, 2007 6:27 pm
Location: Moscow, Russia
Contact:

Re: (ends-with) regex issue

Post by Cyril »

pjot wrote: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)

Code: Select all

> (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
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

You are right, and if it is not a bug, at least it is inconcistent:
> (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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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:

Code: Select all

(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

Locked