Hi all. Here is a tricky little problem for you!
I am writing a script that reads in some textual content in which special characters (newline, tab etc.) have been converted to their string representations. For example, all newline characters in the input have been converted to "\n" (the string literal; literally '\' then 'n'). The input also contains escape sequences such as "\(" and "\)" (parentheses were meta characters in the context where the input content was produced). My script needs to convert the special character sequences back into the original special characters (ie. "\n" needs to be interpretted as the newline character again) and un-escape the meta characters as well. My question is how do I do this?
I have tried a couple of approaches:
my $char = 'n';
$char = sprintf("\\%s", $char);
my $char = 'n';
$char =~ s/(n)/\\$1/;
I would prefer the solution to be general so I don't have to do a regex for each special character (newline, tab etc.) and meta character.
Thanks in advance...
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, details, 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, summary, 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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.