in reply to Re: sequential substitutions
in thread sequential substitutions

I certainly see your point about not trying to parse arbitrary HTML or XML with regular expressions, but in this case, I am looking for a specific tag with numeric values that have already been sanitized by another program.

That does lead me to a side question, though. Do you know of an HTML "cleanup" program (or module) that will take (mostly arbitrary) HTML and output one tag per line properly indented?

Replies are listed 'Best First'.
Re^3: sequential substitutions
by haukex (Archbishop) on Aug 03, 2018 at 11:07 UTC
    I certainly see your point about not trying to parse arbitrary HTML or XML with regular expressions, but in this case, I am looking for a specific tag with numeric values that have already been sanitized by another program.

    Can you trust the source of the XML to never change (whitspace, attributes, CDATA, namespaces, comments, etc.), and can you trust the program that's doing the sanitization to never change its output either? What's wrong with installing modules, especially such helpful ones? (Yes, even you can use CPAN.)

    Sure, it's possible to adapt the regexes shown by the others to this purpose to work on the sample data you've showed, but I personally am not going to jump down the rabbit hole of parsing XML with regexes in this case :-)

    Do you know of an HTML "cleanup" program (or module) that will take (mostly arbitrary) HTML and output one tag per line properly indented?

    Searching for "html tidy" on Google and CPAN shows lots of different options (I haven't used any of them, so I can't give you recommendations). Plus, I wonder why you're asking about such a program, if you said above your XML is already sanitized? ;-)

      Can you trust the source of the XML to never change (whitspace, attributes, CDATA, namespaces, comments, etc.), and can you trust the program that's doing the sanitization to never change its output either? What's wrong with installing modules, especially such helpful ones?

      Yes, I can. The program whose output I am using is only capable of writing about a dozen specific tags, and there will never be any attributes CDATA, namespaces, or such.

      For what I want to do in this specific case, it is just far easier (and more understandable for me) to simply use:

      $fragment =~ s/<foo>\d+<\/foo>/'<foo>' . $i++ . '<\/foo>'/eg;

      I don't need anything more complicated to accomplish this. While I already learned something regarding this, there is no shortage of things I don't know about perl (even having used it for years for quick tasks, I would still classify myself as a novice). Given the increasing popularity of XML, I am certain there will plenty of opportunities for me to learn more advanced techniques in the future, including specialized modules. :)

      Searching for "html tidy" on Google and CPAN shows lots of different options (I haven't used any of them, so I can't give you recommendations). Plus, I wonder why you're asking about such a program, if you said above your XML is already sanitized?

      As I noted, that was a "side question" (meaning it has absolutely nothing to do with what I was asking about sequential substitutions). It is for something completely different, and you seem to know quite a bit about the subject, so I just thought it would be a good time to ask that question. :)

        In regards to pretty-printing XML/HTML, if I'm writing it by hand, I take care to write it cleanly, and if I'm generating it with a module, if I need it to be human-readable, usually those modules have options to format it (e.g. XML::LibXML::Document has ->toString(1) or ->toFile($filename, 1)). Other than that, many IDE's have built-in pretty-printers (when I was coding more Java, I used Eclipse a lot, it's also good for editing/formatting XML).