Hi--
I have a regex puzzle I thought of posting.
I have some saved source code that I would like to extract and reformat to make it "look" like as it appeared in an editor in an HTML way (using <PRE> tags).
Embedded in the code are strings, spaces, tabs, and carriage returns. I thought of blindly replacing all tabs with 4/8 spaces, but then thought of the case where one or two spaces sit right before a tab (and then, what if they do). Because the spaces aren't at a tab boundary, the tab will take precedence as though the spaces don't exist. Also, how many spaces to insert also depends on what column the tab is at.
Examples (* = tabs => 4 spaces):
^**ab*$ next char at col 13
^ **ab*$ next char at col 13, as the space is absorbed
^ * *ab *$ next char at col 17
^ *here is some text***int;$ next char is at 37
I can do this by brute force, but going after this character by character, seems silly and moderately complex. (Though deciphering whatever regex is appropriate lies at the other end of the spectrum).
Most importantly, I don't want to reformat the lines "my way", as I want to retain identical side-by-side sameness.
What's the best way to approach this?
Thanks.
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.