Friends,
I am trying to write a regex for matching
"duration" strings. Basically durations are of the form PnYnMnDTnHnMnS with some extra rules which are described in the link provided. Here's what I have so far.
#!/usr/bin/perl -w
while(<DATA>) {
chomp;
if ( /^-{0,}P{1,1}T{0,}\d{1,}[Y|M|D|H|S]{1,1}/ ) {
print "$_ matches\n";
} else {
print "$_ NO match\n";
}
}
__DATA__
PnYnMnDTnHnMnS
-P1Y
P3Y3M3DT3H3M3S
P3M3Y3DT3H3M3S
P334Y3DT3H3M3S
PT2H
5T
PT
P5M6DT
P5M6DT9
P600T6S
And here's the output I get ...
PnYnMnDTnHnMnS NO match
-P1Y matches
P3Y3M3DT3H3M3S matches
P3M3Y3DT3H3M3S matches
P334Y3DT3H3M3S matches
PT2H matches
5T NO match
PT NO match
P5M6DT matches
P5M6DT9 matches
P600T6S NO match
As you can see my regex is quite cutting it. P5M6DT, P3M3Y3DT3H3M3S, and P5M6DT9 should not match. And tiny brain is fried!
| Plankton: 1% Evil, 99% Hot Gas. |
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.