replace character at definite position

A swapping-ground for Regular Expression syntax

replace character at definite position

Postby transmoderata » Thu Feb 17, 2011 5:58 am

Sounds simple enough: I'm trying to replace a character at a definite position in a filename (not varying from file to file) with another. I.e

anim_101.png
anim_102.png
...]

to

anim_001.png
anim_002.png
...]

I can't seem to find any regular expression for this. Thx
transmoderata
 
Posts: 1
Joined: Thu Feb 17, 2011 5:54 am

Re: replace character at definite position

Postby Admin » Thu Feb 17, 2011 11:44 am

Hi, use Remove + Add (no reg exp)
Admin
Site Admin
 
Posts: 2343
Joined: Tue Mar 08, 2005 8:39 pm

Re: replace character at definite position

Postby Stefan » Thu Feb 17, 2011 4:28 pm

As Admin says

from
anim_101.png
anim_102.png
to
anim_001.png
anim_002.png
do

Count to get the position:
anim_101.png
123456789

and use in your case
Remove(5) From: 6 To: 6
Add(7) Insert: 0 at pos.: 6


###########

If you want to get sure to only replace zero's and only after an underscore, like

from
anim_101.png
anim_102.png
anim_201.png
anim_202.png
anim_301.png
anim_302.png
to
anim_001.png
anim_002.png
anim_201.png
anim_202.png
anim_301.png
anim_302.png
do

RegEx(1)
Match: (.+_)1(.+)
Repla: \10\2
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: replace character at definite position

Postby itsfullofstars » Wed Jan 18, 2012 7:18 am

I have a similar problem with a mix of files.
GG-1234.png
GG1234-hi.png

Need to remove hyphen if it exists at position 3 only

So
GG-1234.png

Needs to be
GG1234.png

But
GG1234-hi.png

Should NOT become

GG1234hi.png
itsfullofstars
 
Posts: 2
Joined: Wed Jan 18, 2012 5:41 am

Re: replace character at definite position

Postby Stefan » Wed Jan 18, 2012 8:22 pm

itsfullofstars wrote:FROM:
GG-1234.png
GG1234-hi.png

TO:
GG1234.png
GG1234-hi.png


Try this with copies of your files:

Code: Select all
RegEx(1)
Match:   ^(..)-(.+)
Repla:   \1\2

Explanation:
^      => Be sure to start regex at start of string
(..)   => match two of any signs, store them in backreferencing group 1
-      => match an literal dash
(.+)   => match one-ore-more of any sign till the end, store in group 2

\1     => replace with what is matched with first expression and stored in group 1
\2     => give back what is stored in group 2


If there is no dash at third position, the regex will not match and so nothing will be changed.
Stefan
 
Posts: 736
Joined: Fri Mar 11, 2005 7:46 pm
Location: Germany, EU

Re: replace character at definite position

Postby itsfullofstars » Wed Jan 18, 2012 11:11 pm

woo! yep that works perfectly.

Thank you so much :)
itsfullofstars
 
Posts: 2
Joined: Wed Jan 18, 2012 5:41 am


Return to Regular Expressions