Regex: \10 vs $10

SomeFileName-15-07-2013 11-10-25.jpg
to read
SomeFileName-15072013 111025.jpg

regex find replace
Find: (.*?)(\s|-)(\d{2})-(\d{2})-(\d{4})(\s|-)(\d{2})-(\d{2})-(\d{2}).(\w{2,4})
Repl: \1\2\3\4\5\6\7\8\9.\ <-then what ?

\10 gets me SomeFileName0 (the \1 plus a '0')

When using a 3rd party tool $10 is 'accepted' showing the following:

You can only use up to 9 capture groups with the normal regexp mode.

If you need more you can use VBScript's regexps instead (see here) assuming they allow it. I haven't tried but VBScript uses the $1 $2 etc. syntax instead of the \1 \2 etc. one, and it seems that regexp engines which use $1 tend to allow $10 while ones which use \1 would interpret \10 as \1 followed by a 0.

(Of course, if you are calling into a rename script then you then have other tools at your disposal which might make more sense than using regular expressions at all. That's up to you.)

Having said all that, if you look at your example, you don't actually need 10 capture groups in the first place. Some of them are right next to each other in both the input and the output, so you could combine them into a single group. You only need to start a new group if you want to remove some characters before that group, or insert the group into the output in a different place. Generally, if you need more than 9 capture groups then there is probably something wrong with the approach or something has been made more complex than it really is.

Thanks Leo, for your elaborate reply.
Frankly, I guess, I am not yet 'ready' for vbscript :wink:
Spent an hour or so moving over the numerous postings, also over some outside gpsoft.
But I am afraid, for someone who hasn't a clue of what of what vbscript is and where to start for the easiest examples,
well, I guess for those exceptional cases I will (have to) use another tool.
Nonetheless, thanks again.

oh, btw, I would not know how to avoid this many groups though, having to remove the '-' characters in the dates.

The thread and script I linked to doesn't need you to understand much about VBScript; you can just edit the regexps inside the script to change what it does.

The most obvious part is that you have two groups at the end which you can combine into one:

You have: (\d{2}).(\w{2,4})
Which you turn into: $9.$10

Instead: (\d{2}.\w{2,4})
Which you turn into: $9

That by itself is enough to make the full thing work in Opus with only 9 groups:

Old name: (.*?)(\s|-)(\d{2})-(\d{2})-(\d{4})(\s|-)(\d{2})-(\d{2})-(\d{2}.\w{2,4})
New name: \1\2\3\4\5\6\7\8\9

You can also improve things more by using non-capture groups for the (\s|-) parts where you don't really need a new group (it's always joined to the things in the two groups on either side of it) and you only have brackets there creating a group because you want to say \s or -. You can use (?:\s|-) instead which lets you use the brackets without creating a new group, saving four groups in total:

Old name: (.*(?:\s|-)\d{2})-(\d{2})-(\d{4}(?:\s|-)\d{2})-(\d{2})-(\d{2}.\w{2,4})
New name: \1\2\3\4\5

Finally, if you are aiming to get all your filenames consistent, then I expect you will always want a - (or a space) between the date and time parts, not whatever happens to be there in the filename at the moment. If so, this will do that:

Old name: (.*(?:\s|-)\d{2})-(\d{2})-(\d{4})(?:\s|-)(\d{2})-(\d{2})-(\d{2}.\w{2,4})
New name: \1\2\3\4-\5\6

[quote="leo"]

Finally, if you are aiming to get all your filenames consistent, then I expect you will always want a - (or a space) between the date and time parts, not whatever happens to be there in the filename at the moment. If so, this will do that:

Old name: (.*(?:\s|-)\d{2})-(\d{2})-(\d{4})(?:\s|-)(\d{2})-(\d{2})-(\d{2}.\w{2,4})
New name: \1\2\3\4-\5\6[/quote]

Thanks again!
This indeed makes a lot of difference.
Will checkout the vbscript link once I have a bit more time.

Have some difficulty finding an 'explanation' as to what ?: is doing though.
Have seen this more often, but could not find a good explanation of what it does, at least it is not mentioned very often on cheatsheets.

Oh, btw, most likely already known to everybody, but nonetheless still worth mentioning here:
when going thru rexegg.com/regex-quickstart.html-> The 1001 ways to use Regex, I stumbled over
"Then check out my awesome Opus Tutorial" (somewhere halfway) referring to dearopus.com/

=

Later

[code]@script vbscript
Option Explicit

Function Rename_GetNewName ( strFileName, strFilePath, _
fIsFolder, strOldName, ByRef strNewName )
' Add script code here
Dim re
Set re = new RegExp
re.IgnoreCase=True
re.Global=True
re.Pattern = "(.(?:\s|-)\d{2})-(\d{2})-(\d{4})(?:\s|-)(\d{2})-(\d{2})-(\d{2}.\w{2,4})"
strNewName = re.Replace(strNewName, "$1$2$3 $4$5$6")
re.Pattern = "(.
(?:\s|-)\d{2})-(\d{2})-(\d{4})(?:\s|-)(\d{6}.\w{2,4})"
strNewName = re.Replace(strNewName, "$1$2$3 $4")
re.Pattern = "(.*(?:\s|-)\d{8})(?:\s|-)(\d{2})-(\d{2})-(\d{2}.\w{2,4})"
strNewName = re.Replace(strNewName, "$1 $2$3$4")

End Function[/code]

You might want to try to wrap a group like "\10" or "$10" into curly brackets, like so "{$10}", to make it easier for the regex-engine to parse or even recognize that group-id.
I don't know if it as an effect here in DO/VBScript, but it does in C#, .NET, Powershell. You more or less escape the regex-group within your mixed string content, which might even be required, if you have groups followed by numerical characters. Think of a string like "$12nd..", is that group#1 or group#12? -> "{$1}2nd.." -> group#1.