regex \\d{n} problem

Q&A's, tips, howto's
Locked
hds1
Posts: 28
Joined: Thu Mar 20, 2014 5:02 pm

regex \\d{n} problem

Post 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

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: regex \\d{n} problem

Post by rickyboy »

Code: Select all

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

hds1
Posts: 28
Joined: Thu Mar 20, 2014 5:02 pm

Re: regex \\d{n} problem

Post by hds1 »

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

Thanks for testing.
Heiko

rickyboy
Posts: 607
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: regex \\d{n} problem

Post by rickyboy »

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

bairui
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China
Contact:

Re: regex \\d{n} problem

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

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: regex \\d{n} problem

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

bairui
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China
Contact:

Re: regex \\d{n} problem

Post by bairui »

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

hds1
Posts: 28
Joined: Thu Mar 20, 2014 5:02 pm

Re: regex \\d{n} problem

Post 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.

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: regex \\d{n} problem

Post 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.)

bairui
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China
Contact:

Re: regex \\d{n} problem

Post 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/

Locked