For a quick-and-dirty regexp solution, how about this...
while ($text =~ m|<jobnumber>(.*?)</jobnumber>.*?<location>(.*?)</loc
+ation>|sg) {
print "Found job number $1 in location $2 \n";
}
NB I'm assuming the missing slashes in the data are a typo. If not then it's easy enough to modify the above.
Of course, a regexp solution isn't the right one if you want it to work in more general cases - like if the tags are the other way round, or whatever. If there's going to be any variation in the data, then the way to go is an XML parser as described by others above.
andy.
Some Time Later: Just For Fun, I tried to see if I could write one that /would/ work with the tags either way round... came up with this...
my $regexp = '<post>';
$regexp .= "(?=.*?<$_>(.*?)</$_>)" foreach qw(jobnumber location);
$regexp .= '.*?</post>';
while ($stuff =~ m|$regexp|sog) {
print "Found job number $1 in location $2 \n";
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|