by Glenn » Thu Jun 15, 2006 10:16 am
One thing which is probably just an oversight (or typo) with the suggested pattern:
(.*) (..).(..).(..)
is that "periods" outside the parentheses really mean any character that isn't a line break character (just like those inside the parentheses) because in a regex it is a wildcard metacharacter. The way it stands, it will match any filename with a space followed by 8 or more characters.
For example, it will match:
name 21.08.04
but it will also match:
name 21.08.04extracrap
name dd.mm.yy evenmore
name namename
8letters
****EDIT: the line above has a space before 8letters but doesn't display in the post
t 8Letters_extraletters
Any name which is longer than 8 letters after the space, it will drop the extra letters and use only those captured in the replacement.
In the last case - i.e. "t 8Letters_extraletters", it will match "t 8Letters" and drop the _extraletters
This would be replaced by "20rs-tt-8L t"
Obviously spambait meant a literal period which would be \.
The regex would then be:
(.*) (..)\.(..)\.(..)
Now it will only match:
name 21.08.04
name 21.08.04extracrap
name dd.mm.yy
name dd.mm.yy evenmore
To get rid of any non numbers in the date positions use \d instead of . in the parentheses
To prevent matching filenames with more than 6 numbers after the space, end the regex with $
i.e. use:
(.*) (\d\d)\.(\d\d)\.(\d\d)$
Finally, a simple way to allow it to match both period or no period separators is to make them optonal with ?
The regex will now be:
(.*) (\d\d)\.?(\d\d)\.?(\d\d)$
This will match:
name 21.08.04
name 210804
and replace them both with:
2004-08-21 name
If you really want the Regex to break a sweat and earn its keep, do the first pass with:
(.*) (\d\d)\.?(\d\d)\.?(?!9[7-9])(\d\d)$
(this tells it to NOT match any dates 97-99)
and replace with:
20\4-\3-\2 \1
This will change all filenames NOT 97-99 to yyyy-mm-dd name format and they won't match the second pass
For the second pass all that are left are 97-99 so use:
(.*) (\d\d)\.?(\d\d)\.?(\d\d)$
and replace with:
19\4-\3-\2 \1
This is still not bullet proof, but I think it's closer to what was intended.
Sorry if I made it more confusing, but it's easy to match more than wanted with these darn regexes
Cordially,
Glenn
Last edited by
Glenn on Sat Jun 17, 2006 5:11 pm, edited 1 time in total.