Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: sequential substitutions

by haukex (Archbishop)
on Aug 03, 2018 at 10:01 UTC ( [id://1219788]=note: print w/replies, xml ) Need Help??


in reply to sequential substitutions

See Parsing HTML/XML with Regular Expressions for why it's not a good idea to do something like this without modules.

use warnings; use strict; use Mojo::DOM; my $xml = <<'ENDXML'; <foo>3</foo> <foo>14</foo> <foo>159</foo> ENDXML my $dom = Mojo::DOM->new->xml(1)->parse($xml); my $i = 1; $dom->find('foo')->each(sub{ $_->content($i++) }); print $dom->to_string; __END__ <foo>1</foo> <foo>2</foo> <foo>3</foo>

Replies are listed 'Best First'.
Re^2: sequential substitutions
by Anonymous Monk on Aug 03, 2018 at 10:42 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.

    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?

      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. :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1219788]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-20 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found