in reply to Re^2: Seeking an Enlightened Path (Parsing, Translating, Translocating)
in thread Seeking an Enlightened Path (Parsing, Translating, Translocating)
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Seeking an Enlightened Path (Parsing, Translating, Translocating)
by nanotasher (Novice) on Mar 11, 2008 at 14:50 UTC |