Copy Chars to Start of Name

A swapping-ground for Regular Expression syntax

Copy Chars to Start of Name

Postby TigerEyes » Sat Aug 23, 2008 1:07 pm

I want to copy the ith character from a file name and insert it at the jth position

assume the file names are of the form "YYYYMM CustomerName Statement"

then I want to copy the character at position 7 and insert at position 0 - so the result would be "CYYYYMM CustomerName Statement"

As I understand the move/copy option, it allows copying/moving characters to and from the ends of the file name, but not from the middle of the file name.

TIA
TigerEyes
 
Posts: 1
Joined: Sat Aug 23, 2008 7:44 am

Re: Copy Chars to Start of Name

Postby Admin » Sat Aug 23, 2008 6:12 pm

You might be better to use Bulk Rename Command, as that allows you to copy sections to the clipboard and then paste them separately.

I don't think a RE will be able to do this.



Jim
Admin
Site Admin
 
Posts: 2351
Joined: Tue Mar 08, 2005 8:39 pm

Re: Copy Chars to Start of Name

Postby Glenn » Mon Oct 20, 2008 4:59 pm

Actually, there is a way to do this using the Regex functionality.
One thing that is a bit unclear to me is your column count.
In the sample it appears that the C is in column 8, not 7 (7 appears to be a space).
At any rate, here is one way to do it.
Using your before and after as an example

Capture the first 7 characters before the one you want copied into one group:
(\w{6} ) Note the space between } and )

the \w means any letter or number
now capture the letter you want to copy

(\w)
This is group 2
Finally, capture The remainder of the filename
(.*)
This is group 3

The end result is:

(\w{6} )(\w)(.*)

Put this in the RegEx(1) Match box.

In the Replace box put:
\2\1\2\3

This will give the result you are looking for.

To better understand, here's how it breaks down:
(I've used | to indicate the ends of chunks of text)

In the Match it gives the following groups

Code: Select all
         |YYYYMM |  |C|     |ustomerName Statement|
         (\w{6} )   (\w)          (.*)
Group       \1      \2             \3

In Replace they are re-ordered

Code: Select all
           |C|  |YYYYMM |  |C| |ustermerName Statement|
Group       \2    \1       \2    \3

Obviously, you can vary the number of characters in each block to suit your needs

Hope this helps,
Glenn
Glenn
 
Posts: 28
Joined: Fri Apr 14, 2006 4:53 pm
Location: Winnipeg, Canada


Return to Regular Expressions