英文:
Using Regular Expression to capitalize all letters at beginning of filenames up to first dash
问题
I can help you with the translation. Here is the translated content:
我在想是否有一种方法可以使用正则表达式来将文件名中的所有字母大写,直到找到第一个短划线为止。
之前:
Apple - Orange - Banana - Filename.txt
Grape - Orange - Banana - Filename.txt
之后:
APPLE - Orange - Banana - Filename.txt
GRAPE - Orange - Banana - Filename.txt
如果可能的话,如果有人可以提供正则表达式,以便大写到第二个短划线,那么可以实现以下效果....
APPLE - ORANGE - Banana - Filename.txt
GRAPE - ORANGE - Banana - Filename.txt
如果需要的话,这也会很有帮助,因为它可以让我了解正则表达式的工作原理。非常感谢任何关于每个部分都在做什么的解释和分解。
我正在使用一个允许用户重命名文件的程序,它提供了两个字段....
第一个字段是要查找的内容,第二个字段是你想要替换它的内容。如果有人能为这两个字段提供正则表达式,那将是很好的。
正则表达式是我认为我理解的东西之一.... 直到我不得不创建另一个
感谢提供的任何帮助
编辑:
感谢大家的回复
不幸的是,我正在使用的程序不支持正则表达式的大小写修改。
我正在使用这个程序的程序是一个名为Double Commander的文件管理器。
然而,这个程序允许使用Lua进行自定义脚本编写,还允许访问任何基于Linux命令行的功能,比如Bash和Linux CL中支持的任何其他内容。
有没有人知道是否可以在上述任何这些脚本语言中实现大小写修改?
我希望这些语言之一支持它,而不需要向它们添加更多语言,例如,我读到Bash如果添加了某种基于Perl的附加功能,则可以支持它,但对我来说,这些东西变得越来越复杂,尽管我似乎已经在Linux命令行中有一些Perl程序可用,所以不确定我的系统上是否已经有一些可用的东西。
英文:
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.
答案1
得分: 1
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.
正则表达式: (.*?)(-.*)
替换字符串: \U$1\E$2
To capitalize up to the second dash:
正则表达式: (.*?-.*?)(-.*)
替换字符串: \U$1\E$2
Explanation
也请查看链接。Regex101提供了非常好的解释,同时也有可视化支持。
.
匹配除了换行符以外的任何字符。(...)
创建一个分组(可以在替换字符串中引用)。*
匹配前一个标记0到无限次。-
精确匹配一个破折号。*?
确保使用懒惰匹配策略。这意味着引擎尝试尽量少地匹配。这就是为什么我们只匹配到第一个破折号之前的第一个单词。\U
开始将后面的所有字符转换为大写。\E
结束转换。$1
插入匹配的第一个分组。
英文:
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: (.*?)(-.*)
<br>
replace-string: \U$1\E$2
<br>
regex101-link.
To capitalize up to the second dash:
regex: (.*?-.*?)(-.*)
<br>
replace-string: \U$1\E$2
<br>
regex101-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. -
\U
start of casting all following chars to uppercase. -
\E
ends the casting -
$1
inserts the first group of the match.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论