I was wondering if there was a way to use Regular Expressions to be able to captialize all letters at the beginning of filenames up to the first dash found.
Before: Apple - Orange - Banana - Filename.txt Grape - Orange - Banana - Filename.txt
After: APPLE - Orange - Banana - Filename.txt GRAPE - Orange - Banana - Filename.txt
If possible, just in case if someone could also provide the regex to capitalize up to the 2nd dash found, so it would achieve the following....
APPLE - ORANGE - Banana - Filename.txt GRAPE - ORANGE - Banana - Filename.txt
This would be helpful if needed as well allowing me to maybe understand what is being done by the regex. Any breaking down and explaining what each piece of is doing would be greatly appreciated.
I am using a program that allows user to rename files, where it gives two fields.... 1st field is a what to look for and the 2nd field is a what would you like to replace it with kind of approach. If this makes sense, if someone could provide regex for both of the fields that would be great.
Regular Expressions is one of those things I think I understand.... until I have to create another one :)
Thank you for any help offered
EDIT: Thank you all for your responses Unfortunately the program I am using this in does not support case modification with Regex. The program I was using this in is a file manager called Double Commander. This program however does allow for the use of custom scripting using Lua and also allows access to any Linux command line based functions, so things like Bash and anything else supported in Linux CL. Does anyone know if the case modifications can be achieved in any of these scripting languages mentioned above?
I was hoping that one of these languages would just support it without need for adding more languages to them, for example I read that Bash can support it if you have some kind of Perl based addition added to it, but stuff like this gets more and more complicated for me, although I seem to have some kind of Perl program available in Linux command line already so not sure if something is already available on my system.
Whether my suggestion works fully depends on how regex is implemented in the program you're using. If it uses the javascript regex engine, it will not work. If it uses the PHP regex engine, it will work. If your program doesn't support the case modifiers (\U, \L), it will not be possible.
regex:
(.*?)(-.*)replace-string:
\U$1\E$2regex101-link.
To capitalize up to the second dash:
regex:
(.*?-.*?)(-.*)replace-string:
\U$1\E$2regex101-link.
Explanation
Also take a look at the links. Regex101 has really good explanations, that are also supported visually.
.matches any character except line breaks(...)creates a group (that can later be referenced in the replace-string)*matches the previous token 0-infinity times.-matches exactly one dash.*?ensures a lazy matching strategy. Meaning the engine tries to match as little as possible. That's why we only get the first word until the first occurring dash.\Ustart of casting all following chars to uppercase.\Eends the casting$1inserts the first group of the match.