Consider the following example of text extraction.
lst = {
"foo bar <span>feyenoord - ADO</span>",
"foo bar foo <span>Chelsea - Leicester</span>",
"foo bar <span id=\"puin\">feyenoord - ADO Den Haag</span>"};
The goal is to take the text in the span element.
Map[StringTake[StringCases[#, ">" ~~ __ ~~ "<"], {2, -2}] &, lst]
The result is as expected.
{
{"feyenoord - ADO"},
{"Chelsea - Leicester"},
{"feyenoord - ADO Den Haag"}
}
Then I received a somewhat different input text. Note the ">" following "foo" in the third element.
lst1 = {
"foo bar <span>feyenoord - ADO</span>",
"foo bar foo <span>Chelsea - Leicester</span>",
"foo> bar <span id=\"puin\">feyenoord - ADO Den Haag</span>"};
The goal is still to take the text in the span element.
Map[StringTake[StringCases[#, ">" ~~ __ ~~ "<"], {2, -2}] &, lst1]
But now, the result is not as expected.
{
{"feyenoord - ADO"},
{"Chelsea - Leicester"},
{" bar <span id=\"puin\">feyenoord - ADO Den Haag"}
}
Whatever texts are received, i.e.:
"foo bar <<< foo <span id="id" attr="name">payload</span> foo>span>>>"
it is always "payload", the innertext of the span element that needs to be extracted.
I thought about removing id="__" but other attributes may come in at other times.
So I am basically looking ( and not wanting to reinvent the wheel ) for the regular expression or Mathematica string expression to extract the innertext of an HTML element ( span in this case ).
Note that the strings are not valid HTML.
span
or other HTML tags, you can probably write a more specialized regexp for it, but it certainly has to be more specific than the one you are using. This has nothing to do with Mathematica per se - you would have to construct it for whatever language you'd use for extraction. Or you could use one of the proper HTML parsers, and then postprocess the result symbolically. $\endgroup$StringCases["foo> bar <span id=\"puin\">feyenoord - ADO Den Haag</span>", "<span" ~~ Shortest[__] ~~ ">" ~~ x : Shortest[__] ~~ "</span" :> x]
? $\endgroup$