0

Im trying to add a "." after the last occurrence of a integer starting backwards from ".html"

python

import os import glob import re for name in glob.glob('*.html'): newname = re.compile('name(.*?)(\d+)') os.rename(name, newname) 

change filenames from:

1cor12.html gen1.html 

to:

1cor.12.html gen.1.html 

3 Answers 3

3

Something like should do:

re.sub('(\d+)(?=\.html)', r'.\1', s) 
Sign up to request clarification or add additional context in comments.

1 Comment

This also does not work if the digits don't immediately follow the .html.
2

Is this what you want?

re.sub(r'(\d+\.html)',r'.\1',r'1cor12.html') 

Note, this only works if you want numbers immediately preceding ".html". In other words, this will do nothing to "gen1a.html".

If you want to match the latter case ('gen1a.html') you could probably do something like:

re.sub(r'(\d+\D*\.html)',r'.\1',r'1cor12.html') 

5 Comments

That won't work if the digits don't immediately precede the .html.
the digits always come before .html but did not change "gen1.html" to "gen.1.html"
@kojiro -- Yeah, I know. I was assuming the problem was a little easier than stated (hence the "is this what you what?"), but I suppose I'll document that.
@kojiro -- Fixed so it will work if digits don't immediately precede the .html
@mgilson nicely done. It even matches 1a2b3c.html, returning 1a2b.3c.html, which is exactly what OP asked for. Too bad he didn't ask for what he actually wanted.
0
newname = "%s.%s.html" % re.match(r"(.*)(\d+)\.html", oldname).groups() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.