0
$\begingroup$

I have an html-code which should be analyzed with String Patterns.

I would like to find all p-tags which contain a padding-left. And I would like to read out the value of the padding left.

I have defined a

regPx = RegularExpression["\\d+"];

It seems to work when I try to search the html by

listPx = 
 StringCases[normText[[n]], "padding-left: " ~~ x : regPx -> x]

But, I would like to find only those values which begin in a new p-tag. That's why I tried it with

listPx = 
 StringCases[normText[[n]], 
  "<p " ~~ ___ ~~ "padding-left: " ~~ x : regPx -> x]

And this brings me strange results.

How do I have to define the pattern so that it works?

THX and best regards, JJJ

$\endgroup$
2
  • $\begingroup$ Sorry, you're right. I forgot to add it. But I have solved the problem (see my own answer below). But could you please have a look onto my comment to my answer? I got another issue... $\endgroup$
    – JJJanezic
    Commented Nov 6, 2022 at 9:17
  • $\begingroup$ Got is. See below. $\endgroup$
    – JJJanezic
    Commented Nov 6, 2022 at 9:28

2 Answers 2

2
$\begingroup$

I suggest not using some custom-written regex for parsing the whole HTML but a built-in importer for HTML instead.

(* Auxiliary function for extracting style values *)
extractValue[prop_][style_] := 
  First@Cases[
    StringSplit[#, ":"] & /@ StringSplit[style, ";"], {prop, val_} :> 
     val];

(* Find elements with given tag and given prop in style *)
findElements[doc_, tag_, prop_] := 
 extractValue[prop] /@ 
  Cases[doc, 
   XMLElement["p", {"style" -> style_, ___}, ___] /; 
     StringContainsQ[style, "padding-left"] :> style, Infinity]

(* Example HTML *)
html = "<html><body><div><p style=\"padding-left:1em;margin-left:2em\">Text</p></div> \
        <p style=\"color:red;padding-left:5px;\">Text</p><p style=\"color:blue\">Text</p> \
        </body></html>";

(* Import and parse HTML *)
doc = ImportString[html, {"HTML", "XMLObject"}];

(* Find p elements containing padding-left *)
findElements[doc, "p", "padding-left"]

{"1em", "5px"}

$\endgroup$
0
$\begingroup$

As written above, I have managed to analyze a string like this

<p style=""padding-left: 30px;""><span class=""bullet1"">1.</span>Text.</p>

with

listPx[[n]] = StringCases[normText[[n]], Shortest["<p " ~~ ___ ~~ "padding-left: " ~~ x : regPx___ ~~ "px" ~~ ___ ~~"<span class=\"bullet1\">" ~~ y : regText___ ~~ "</span>"] -> {x, y}];

so that I get back {30,1}.

The two regex used are defined as follows:

regPx = RegularExpression["\\d+"];
regText = RegularExpression["\\(?\\w+\\.?\\)?"];

But now, I have another string that contains a bit more information in the html-tag:

<p style=""padding-left: 30px;""><span class=""bullet1""><strong>&sect; 18.</strong> (1)</span>Text</p>

So, the additional information is the xxx tag.

How could I change my above mentioned string pattern, so that it "ignores" the strong-tag? I mean, if it's there: ignore it. If it isn't there, just go ahead. In both cases return the same as above: {30,1}

I just forgot to add "Shortest[]", so it went from the first occurrence of "<p " to the last of "padding-left: ".

The correct solution therefore is:

listPx = StringCases[normText[[n]],Shortest["<p " ~~ ___ ~~"padding-left: " ~~ ___ ~~ "px" ~~ ___ ~~"<span class=\"bullet1\">" ~~ ___ ~~ "</span>"]]

To combine it with RegEx this reads as follows:

listPx = StringCases[normText[[n]],Shortest["<p " ~~ ___ ~~"padding-left: " ~~ x : regPx___ ~~ "px" ~~ ___ ~~"<span class=\"bullet1\">" ~~ y : regText___ ~~ "</span>"] -> {x, y}]

THX, JJJ

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.