regex \\d{n} problem
regex \\d{n} problem
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
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
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)
Re: regex \\d{n} problem
odd, thought i checked the longer seq as well ... to many hours today.
Thanks for testing.
Heiko
Thanks for testing.
Heiko
Re: regex \\d{n} problem
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)
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
Re: regex \\d{n} problem
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
Aw, come now, Cormullion... Regular Expressions are beautiful and powerful.
Re: regex \\d{n} problem
@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.
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.
-
- Posts: 2038
- Joined: Tue Nov 29, 2005 8:28 pm
- Location: latiitude 50N longitude 3W
- Contact:
Re: regex \\d{n} problem
@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
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/
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/