regex \\d{n} problem

Q&A's, tips, howto's

regex \\d{n} problem

Postby hds1 » Thu Mar 20, 2014 5:12 pm

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
hds1
 
Posts: 27
Joined: Thu Mar 20, 2014 5:02 pm

Re: regex \\d{n} problem

Postby rickyboy » Thu Mar 20, 2014 6:28 pm

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)
rickyboy
 
Posts: 595
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: regex \\d{n} problem

Postby hds1 » Thu Mar 20, 2014 6:43 pm

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

Thanks for testing.
Heiko
hds1
 
Posts: 27
Joined: Thu Mar 20, 2014 5:02 pm

Re: regex \\d{n} problem

Postby rickyboy » Thu Mar 20, 2014 6:48 pm

:) I feel that! Welcome!
(λx. x x) (λx. x x)
rickyboy
 
Posts: 595
Joined: Fri Apr 08, 2005 7:13 pm
Location: Front Royal, Virginia

Re: regex \\d{n} problem

Postby bairui » Thu Mar 20, 2014 11:09 pm

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)
bairui
 
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China

Re: regex \\d{n} problem

Postby cormullion » Fri Mar 21, 2014 5:48 pm

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)
cormullion
 
Posts: 2037
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W

Re: regex \\d{n} problem

Postby bairui » Fri Mar 21, 2014 10:45 pm

Aw, come now, Cormullion... Regular Expressions are beautiful and powerful.
bairui
 
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China

Re: regex \\d{n} problem

Postby hds1 » Sat Mar 22, 2014 7:42 am

@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.
hds1
 
Posts: 27
Joined: Thu Mar 20, 2014 5:02 pm

Re: regex \\d{n} problem

Postby cormullion » Sat Mar 22, 2014 10:27 am

@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.)
cormullion
 
Posts: 2037
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W

Re: regex \\d{n} problem

Postby bairui » Sat Mar 22, 2014 10:50 am

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/
bairui
 
Posts: 64
Joined: Sun May 06, 2012 2:04 am
Location: China


Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 3 guests

cron