regex \\d{n} problem

Started by hds1, March 20, 2014, 10:12:04 AM

Previous topic - Next topic

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 "Exactn"} else {print "Nopen"};'



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



What am i missing here ?



Regards

Heiko

rickyboy

#1
$ perl -e 'my $ttt="12345678"; if($ttt =~ /d{4}/) {print "Exactn"} else {print "Nopen"};'
Exact
(λx. x x) (λx. x x)

hds1

#2
odd, thought i checked the longer seq as well ... to many hours today.



Thanks for testing.

Heiko

rickyboy

#3
:)  I feel that!  Welcome!
(λx. x x) (λx. x x)

bairui

#4
As with all regex engines, use anchors (like ^ and $) to restrict the match to start/end boundaries:


> (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)

cormullion

#5
(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)

bairui

#6
Aw, come now, Cormullion... Regular Expressions are beautiful and powerful.

hds1

#7
@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.

cormullion

#8
@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 https://github.com/cormullion/newlisp-projects/blob/master/markdown.lsp">markdown.lsp. I can't help feeling there's a better way.)

bairui

#9
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/