Page 1 of 1

regex \\d{n} problem

Posted: Thu Mar 20, 2014 5:12 pm
by hds1
Hello,

i'am trying to figure out the logic behind the newlisp regex system.

> (regex "\\d{3}" "12343243242")
("123" 0 3)

I would consider this as not correct, as i understand that \d{3} shall hit "exactly" three digits, only three digits in number and term and not the first three found.
Perl's PCRE handles this correctly. If you try
perl -e 'my $ttt="1234"; if($ttt =~ /\d{4}/) {print "Exact\n"} else {print "Nope\n"};'

Using: newLISP v.10.5.4 64-bit on Linux IPv4/6 UTF-8, options: newlisp -h

What am i missing here ?

Regards
Heiko

Re: regex \\d{n} problem

Posted: Thu Mar 20, 2014 6:28 pm
by rickyboy

Code: Select all

$ perl -e 'my $ttt="12345678"; if($ttt =~ /\d{4}/) {print "Exact\n"} else {print "Nope\n"};'
Exact

Re: regex \\d{n} problem

Posted: Thu Mar 20, 2014 6:43 pm
by hds1
odd, thought i checked the longer seq as well ... to many hours today.

Thanks for testing.
Heiko

Re: regex \\d{n} problem

Posted: Thu Mar 20, 2014 6:48 pm
by rickyboy
:) I feel that! Welcome!

Re: regex \\d{n} problem

Posted: Thu Mar 20, 2014 11:09 pm
by bairui
As with all regex engines, use anchors (like ^ and $) to restrict the match to start/end boundaries:

Code: Select all

> (regex {\d{3}} "12343243242")
("123" 0 3)
> (regex {^\d{3}} "12343243242")
("123" 0 3)
> (regex {\d{3}$} "12343243242")
("242" 8 3)
> (regex {^\d{3}$} "12343243242")
nil
> (regex {^\d{3}$} "123")
("123" 0 3)

Re: regex \\d{n} problem

Posted: Fri Mar 21, 2014 5:48 pm
by cormullion

Code: Select all

(replace "[^a-z]" "@$@&i&&@&$@h$$a@@&%&@%t@@%@%@e%%&@@$r@$%%e&&%&g@%%u$&&%l&@a&%$r@$%e@%x%&p&r&@$@e&$&ss&$i%&o%%%@ns"{}0)

Re: regex \\d{n} problem

Posted: Fri Mar 21, 2014 10:45 pm
by bairui
Aw, come now, Cormullion... Regular Expressions are beautiful and powerful.

Re: regex \\d{n} problem

Posted: Sat Mar 22, 2014 7:42 am
by hds1
@bairui

thanks for reminding me .... i've probably to many anchors in the brain activated.
A fool and his strings are soon parted.

> (regex {^\d{3}$} "12343243242")
nil

That is the one i've been looking for.

Re: regex \\d{n} problem

Posted: Sat Mar 22, 2014 10:27 am
by cormullion
@bairui yes, their power is undisputed. Beauty? Well, in a way, perhaps. But the reason I hate them is because they make me feel stupid. :) I can write a pattern which works, but when I look at it later I can't understand it at all, and I can't read it fluently. (Have a look at markdown.lsp. I can't help feeling there's a better way.)

Re: regex \\d{n} problem

Posted: Sat Mar 22, 2014 10:50 am
by bairui
Heh... I see, Cormullion. I felt that way about Perl before leaving her for Ruby (before newLISP and I met). Perl had a way of making you feel like a mad genius one day, and the village idiot the next.

Jeffrey Friedl's Mastering Regular Expressions is a *really* good read. I highly recommend it.

@hds1: you're welcome. Finally something on these forums I can actually answer! \o/