I am no good with regex, but the solution is a viable one if I can configure the number of characters (instead of *). For instance, if I could do something like:
$_ =~ s/(\D[1-10])(\D[11-16]) ... /$3$4$1$2/g;
That would work for input. Could I configure the second part of the search dynamically?
| [reply] [d/l] |
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;
| [reply] [d/l] [select] |
In your example, you have:
$_ =~ s/(.{10})(.{4})(.{6})(.{6})/$3$2$4$1/g;
I understand your regex pattern, but I'm concerned about the second part. Would there be a way to make this dynamic? For instance, putting in a string based on what I read in from the config file? (let's say the config file wants $3 as the first parameter, then $2 as the second parameter, then the result of some function with $4 as input into that function as the third parameter, etc...).
Sorry if it seems like I'm useless. :) Perl isn't my first language, and it has been hard adjusting to the new syntax.
| [reply] |