Mailinglist Archive: opensuse-programming (68 mails)
| < Previous | Next > |
Re: [suse-programming-e] Tips for PCRE expressions?
- From: Mark Gray <markgray-temp-1072781312@xxxxxxxxxxxx>
- Date: 08 Jan 2004 11:50:31 -0500
- Message-id: <6soetev0yw.fsf@xxxxxxxxxxxx>
Trey Gruel <drathos-suse@xxxxxxxxxxxxxx> writes:
>> Secondly, it is almost always a mistake to try to do all your work
>> using a single regex -- use multiple regex's and program logic based
>> on the results.
> One of the reasons I'm trying to move things to regex is so the simpler
> checks can be edited in config files that can be reloaded on the fly.
Have you considered embedding a perl interpreter in your program? Not
such overkill if you have a lot of configuration that you would like
to be that flexible. (For one regex, it would be overkill though:-)
> If this check ends up being a problem, I may end up making an option for a
> "negative check" where it'll match invalid values. Prolly not a bad idea
> anyway..
This is what the perl cookbook 2nd edition (page 61) gives for a VALID
C float:
^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$
and pcretest seems to like it:
markgray@soyo:/ARK/BOOKS/perlcookbook2/ch02> pcretest
PCRE version 4.4 21-August-2003
re> /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
data> 123.45
0: 123.45
1:
2: .45
data> -123.45
0: -123.45
1: -
2: .45
data> unkn
No match
data> NaN
No match
data> abcdef
No match
data> +123.456e+02
0: +123.456e+02
1: +
2: .456
3: e+02
4: +02
data>
>> Secondly, it is almost always a mistake to try to do all your work
>> using a single regex -- use multiple regex's and program logic based
>> on the results.
> One of the reasons I'm trying to move things to regex is so the simpler
> checks can be edited in config files that can be reloaded on the fly.
Have you considered embedding a perl interpreter in your program? Not
such overkill if you have a lot of configuration that you would like
to be that flexible. (For one regex, it would be overkill though:-)
> If this check ends up being a problem, I may end up making an option for a
> "negative check" where it'll match invalid values. Prolly not a bad idea
> anyway..
This is what the perl cookbook 2nd edition (page 61) gives for a VALID
C float:
^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$
and pcretest seems to like it:
markgray@soyo:/ARK/BOOKS/perlcookbook2/ch02> pcretest
PCRE version 4.4 21-August-2003
re> /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/
data> 123.45
0: 123.45
1:
2: .45
data> -123.45
0: -123.45
1: -
2: .45
data> unkn
No match
data> NaN
No match
data> abcdef
No match
data> +123.456e+02
0: +123.456e+02
1: +
2: .456
3: e+02
4: +02
data>
| < Previous | Next > |