From what I understood from your example, you wanted to switch the places of sequences of letters and sequences of numbers. In the example solution I gave you, the regular expression will be looking for four of those sequences regardless of the number of characters in each sequence (provided those sequences are in alternate order).That regexp is not looking for the positions of the characters in the line.

From your example I saw a sequence of letters, or non-digits, so used the \D wildcard that matches non-digits. Then there is a sequence of digits, and the wildcard that matches digits is \d. Since there will be the need to switch the positions of those sequences, there is the need to capture them with () for later use.

The example as explained above does not know the number of characters on each sequence, but if you do want to do the rearrangement based on particular positions in the line, there are alternatives that take that into account (besides the excellent one BrowserUK showed).

If you say you have 10 characters, then 4, then 6 and finally 6 more, we can write such a regexp. For that we will use the . (Match any character), {n} (Match exactly n times) and the grouping (). The regexp would be:

$_ =~ s/(.{10})(.{4})(.{6})(.{6})/$3$2$4$1/g;

In reply to Re^3: Seeking an Enlightened Path (Parsing, Translating, Translocating) by olus
in thread Seeking an Enlightened Path (Parsing, Translating, Translocating) by nanotasher

Title:
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, 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:
    & &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.