Hi,
my problem:
I've got a single string, containing the contents of a complete HTML-File
(including CarriageReturns). This string contains several <table>...<\table>
parts (with a lot of linebreaks in the ... part).
What I want to do is to get the first table from the string.
What I tried:
my $html="<table>\nt1\n<\/table>\n<table>\nt2\n<\/table>\n"
$html =~ m/<table>(.*?)</table>/;
# Failed, since 'The period '.' matches any character but ``\n''
$html =~ m/<table>(.*?)</table>/s;
# SUCCESS, since 's modifier (//s): Treat string as a single long
# line. '.' matches any character, even "\n"'
$html =~ m/<table>[a-zA-Z0-9 \r\n<>"!-=]*?</table>
# SUCCESS - Emulating '.' with defining a own character class,
# containing 'any' characters (or a subset in this example) including
# '\n'
# NO s modifier (//s) needed
$html =~ m/<table>[.\n]*?</table>;
# Same as above, but using '.' instead of explicitly listing all
# characters ...
# Failed, WHY? Why is '.' not allowed within a character class?
Further investigation shows, that
[.] is not a valid character class ...
My questions are:
Why is '.' not allowed within a character class?
It's clear to me now that my desired character class
[.\n] can be achieved with
the s modifier - but why is there such an "inconsistent way" using a modifier to emulate a character class?
Why is there no "super" character class - matching ALL characters including '\n'?
What's the reason excluding '\n' from '.'? (Why is '\n' handled in a special way?)
Hoppfrosch
Edit by castaway - use html entities instead of angled brackets
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.