How do you do "move (from # to #) chars to position #"

A swapping-ground for Regular Expression syntax

How do you do "move (from # to #) chars to position #"

Postby argitoth » Tue Aug 13, 2013 6:03 am

What would the regular expression be if I want something like:

from
aaa bbb ccc ddd

to
aaa ccc bbb ddd

example logic: move all characters between the 4th to the 8th character to position 12 and separate by space


Thank you!
argitoth
 
Posts: 4
Joined: Tue Aug 13, 2013 5:57 am

Re: How do you do "move (from # to #) chars to position #"

Postby Stefan » Tue Aug 13, 2013 9:48 am

argitoth wrote:What would the regular expression be if I want something like:

from
aaa bbb ccc ddd

to
aaa ccc bbb ddd

example logic: move all characters between the 4th to the 8th character to position 12 and separate by space


Thank you!



>>example logic: move all characters between the 4th to the 8th character to position 12 and separate by space
FROM:
ABCDxxxHIJKLmnop.ext
TO:
ABCDHIJKLxxxmnop.ext

RegEx(1)
"between the 4th to the 8th character > char 5+6+7=3
"to position 12 > 12-8=4 +1 for 'behind' =5
Match: (.{4})(.{3})(.{5})(.+)
"move all characters ... to position 12 and separate by space
Repla: \1\3 \2 \4
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: How do you do "move (from # to #) chars to position #"

Postby argitoth » Tue Aug 13, 2013 6:52 pm

breaking it down for my learning:

() = group boundaries
{} = {} (not actual regex code)
{#} = repeat previous code # times, {} is only RegEx code if there is RegEx stuff inside.
. = represets a-z A-Z 0-9 and the rest of possible chars. Period means match any character?

(.{4}) = first group has 4 chars, or match any character 4 times inside the first group
(.{3}) = second group is the next 3 chars after the first group
(.{5}) = third group is the next 5 chars after the second group
(.+) = fourth group has the rest of the chars after the third group.

correct?
argitoth
 
Posts: 4
Joined: Tue Aug 13, 2013 5:57 am

Re: How do you do "move (from # to #) chars to position #"

Postby Stefan » Wed Aug 14, 2013 7:13 am

(...) parentheses HERE are used to store that what is matched by our expression.
That can be used as backreferences in the replacement by using '\1' signs.


A dot means: ONE piece of ANY SIGN: a-z, A-Z, other chars, 0-9, whitespace,
punctuation and signs ._-\/<>+#[(..., even line breaks in text editors (if a option is set)

{...} means: match the previous used expression (the dot in our case, which means: one piece of any sign) that many times.


So: (.{4}) means: match 4 pieces of any sign and store that what is matched in a backreference group for later reuse.

See the end of the first thread in the RegEx sub forum for more about this stuff.



Please wait a few seconds then submit. Thank you.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU


Return to Regular Expressions