Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

G'day RonW,

The first thing to do, to make your code more readable and maintainable, is to assign those regex fragments with the escapes to meaningful named variables.

An '@' character doesn't need to be escaped in a character class, so you can start with something like:

my $any_slash_or_at = qr{ [\\@] }x;

In my code below, you'll see that having done that means the rest of the code has almost no backslashes at all. This hopefully makes the code a lot more readable now and, six months or more down the track, when you or someone else needs to make a change.

I then started to build up more complex regexes based on $any_slash_or_at. Again, this helps with readability and future maintenance.

I ran a test, replacing your

s/(?<![\\\@])[\\\@]\@/\x11/g; s/(?<![\\\@])[\\\@]\\/\x12/g;


s/$slash_or_at__at/DC1/g; s/$slash_or_at__slash/DC2/g;

The ouput looks fine: I'll leave you to make similar changes in the while loop.

You asked about "a better way". Building up the regex from fragments hopefully goes some way towards this. I've provided an alternative which uses a less complex regex (although still built from the initial $any_slash_or_at fragment) and requires only a single substitution: if nothing else, that gives you another option.

[Everything I've provided should work with Perl v5.8 — there may be better solutions if you have a more recent Perl version. As I didn't know what version you're working with, I didn't persue these other potential solutions.]

Here's the test script:

#!/usr/bin/env perl -l use strict; use warnings; my $test_string = '\\\\\\ \\\\@ \\@\\ \\@@ @\\\\ @\\@ @@\\ @@@ \\\\ \\@ @\\ @@ \\ +@'; my $any_slash_or_at = qr{ [\\@] }x; my $lone_slash_or_at = qr{ (?<!$any_slash_or_at) $any_slash_or_at }x; my $slash_or_at__at = qr{ $lone_slash_or_at \@ }x; my $slash_or_at__slash = qr{ $lone_slash_or_at \\ }x; # Your code - a lot easier to read print 'Testing your code:'; $_ = $test_string; print; s/$slash_or_at__at/DC1/g; s/$slash_or_at__slash/DC2/g; print; # An alternative with a less complex regex and a single substitution my %replace = ( '@' => 'DC1', '\\' => 'DC2' ); my $escape_slash_or_at = qr{ $any_slash_or_at ( $any_slash_or_at ) }x; print 'Testing simultaneous replacements:'; $_ = $test_string; print; s/$escape_slash_or_at/$replace{$1}/g; print;


Testing your code: \\\ \\@ \@\ \@@ @\\ @\@ @@\ @@@ \\ \@ @\ @@ \ @ DC2\ DC2@ DC1\ DC1@ DC2\ DC2@ DC1\ DC1@ DC2 DC1 DC2 DC1 \ @ Testing simultaneous replacements: \\\ \\@ \@\ \@@ @\\ @\@ @@\ @@@ \\ \@ @\ @@ \ @ DC2\ DC2@ DC1\ DC1@ DC2\ DC2@ DC1\ DC1@ DC2 DC1 DC2 DC1 \ @

-- Ken

In reply to Re: better way to escape escapes by kcott
in thread better way to escape escapes by RonW

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (5)
    As of 2018-05-26 22:26 GMT
    Find Nodes?
      Voting Booth?