The {\\.([^./]+)$}
you found is not a functioning regex - in so far as {
and }
are not used like that in regex. It's possible some language uses those as delimiters for a regex, but in actual regex they are used for quantifiers, (for example x{3,5}
matches between 5 and 3 of x).
Also, with \\.
that is looking for a literal \
followed by .
as a metacharacter (which is not the intent - it should just be \.
). (Again this may be due to whatever language that example came from requiring backslashes to be escaped; CF doesn't require it, and thus doing so interferes with the regex behaviour.)
Regex solution:
rereplace( filename , '(?=\.[^./]+$)' , '.' & mtime )
This uses a lookahead (?=..)
instead of a capturing group to simplify the replacement side - the replacement is inserted at the position before the final .
without replacing any actual characters.
If it ever comes to replacing the timestamp, it's easy to add \.\d+
to the start and have it just work.
Non-regex solution:
Left(filename,filename.lastIndexOf('.')+1) & mtime & '.' & ListLast(filename,'.')
Potentially more efficient (but not necessarily to a significant level).
One of the downsides of regex is that they're one-way - they start at the beginning and go forward. For long strings when you only care about the last few characters that can mean wasting time looking at characters you don't care about. Methods like lastIndexOf
and ListLast
work backwards from the end, and thus avoid those unnecessary inspections. Again, in this situations the strings are short enough it almost certainly wont matter, but it is something worth keeping in mind.