|Perl: the Markov chain saw|
Re: better way to escape escapesby kcott (Abbot)
|on Apr 16, 2014 at 03:00 UTC||Need Help??|
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:
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
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: