http://qs321.pair.com?node_id=934556


in reply to A regex question

roboticus has shown how to deal with the multi-line para problem, so I'm omitting that. But as the AM above suggests, using an appropriate module or modules can save you much grief.

What follows is NOT code to adopt or emulate; rather it is intended to suggest the pitfalls you may encounter in dealing the html. Web pages may be fully compliant, well-structured (for readability and maintenance); and reasonably consistent within a <ol>, <ul>, or <table> (and those are just "fer'instances") but don't bet the farm on that!

That which you're scraping may well be less than well-formed, compliant or consistent. And that makes parsing difficult. This bad example confines itself to some minor variance and only in the rendered portion of a link:

#!/usr/bin/perl use Modern::Perl; # 934485 my ($name, @names, @scrapings, $scraping); while ( <DATA> ) { $scraping = $_; push @scrapings, $scraping; } for $scraping(@scrapings) { $scraping =~ /(\d\w+ # begin capture, num followed by 1 or more +wordchars [. ]* # charclass of dot or space (?: # non-capture grouping \w+ # wordchars [. ]+ # one or more of a literal dot or + space )* # group is OPTIONAL by quantifyin +g it as "0 or more" [\w-]+ # wordchars or hyphens \s* # zero or more spaces ) # end capture before <\/a> # close link_renderable and href /x; # extended format if ($1) { $name = $1; push @names, $name; } } for $name (@names) { say $name; } say "\n\t Done"; __DATA__ <li><a href="page.aspx?1ar3xlr29">1Mary Mary QuiteContrary </a></li> <li><a href="page.aspx?43xlr17">2Sam Samuels</a></li> <li><a href="page.aspx?3719rlr17qt">3Joe.Bones</a></li> <li><a href="page.aspx?a=c4bc46eswsdw32fcc">4John.Martines </a></li> <li><a href="page.aspx?a=0a2b-a99d-3754eb2f5e35">5Mary Jones</a></li> <li><a href="page.aspx?a=1ef7b100-8dc4-4b40-871c-68b1d0">6Fernando Pra +deras</a></li> <li><a href="page.aspx?a=e8ec1d77-ee83-4797-b9c4-7676053a4926"> 7blifs +tik</a></li> <li>foobar baz blivitz <a href="page.aspx?a=e8ec1d77" class="b">8Frede +rick B. Ohlmsted</a> more blah blah blah and so on ad nauseum....</l +i> <li><a href="page.aspx?a=c4bc46eswsdw32fcc">9Ernesto Maria Santiago-Co +rtez</a></li>

Executing that script produces what I believe to be data consistent with your spec:

1Mary Mary QuiteContrary 2Sam Samuels 3Joe.Bones 4John.Martines 5Mary Jones 6Fernando Praderas 7blifstik 8Frederick B. Ohlmsted 9Ernesto Maria Santiago-Cortez Done

But look at the hoops we jumped thru to get here -- for what are fundamentally small-potatoes differences in the style of the names. Strong Suggestion: Don't do it this way.