英文:
Query regarding regex format for specific filename
问题
我需要将一些文件重命名 - 想到使用正则表达式自动化这个过程。
当前的文件名格式是:
CompanyName-Invoice-04-Date-2021-01-06-<b>Client-Abra</b>-<b>Paid</b>
CompanyName-Invoice-20-Date-2022-10-10-<b>Client-Abra-Cadabra</b>-<b>Paid</b>
CompanyName-Invoice-99-Date-2023-12-31-<b>Client-Boombastic-Fantastic</b>-<b>Paid</b>
我想将文件名格式更改为:
CompanyName-Invoice-04-<b>Client-Abra</b>-Date-2021-01-06-<b>Paid</b>
CompanyName-Invoice-20-<b>Client-Abra-Cadabra</b>-Date-2022-10-10-<b>Paid</b>
CompanyName-Invoice-99-<b>Client-Boombastic-Fantastic</b>-Date-2023-12-31-<b>Paid</b>
我已经成功将日期部分分组,使用以下正则表达式:
^([A-Za-z0-9-]{17})([-]{1})([A-Za-z0-9-]{15})([-]{1})
我有意将 '-' 分开,以便以后如果需要的话可以操作它。
当涉及到分组 '客户名称' 和 'Paid' 部分时,我不知道怎么做。
非常感谢任何帮助分组最后两部分的支持。
非常感谢!
英文:
I have to rename some files - thought of automating the process using regex.
The current filename format is:
<pre>
CompanyName-Invoice-04-Date-2021-01-06-<b>Client-Abra</b>-<b>Paid</b>
CompanyName-Invoice-20-Date-2022-10-10-<b>Client-Abra-Cadabra</b>-<b>Paid</b>
CompanyName-Invoice-99-Date-2023-12-31-<b>Client-Boombastic-Fantastic</b>-<b>Paid</b>
</pre>
I want to change the filename format to:
<pre>
CompanyName-Invoice-04-<b>Client-Abra</b>-Date-2021-01-06-<b>Paid</b>
CompanyName-Invoice-20-<b>Client-Abra-Cadabra</b>-Date-2022-10-10-<b>Paid</b>
CompanyName-Invoice-99-<b>Client-Boombastic-Fantastic</b>-Date-2023-12-31-<b>Paid</b>
</pre>
I have managed to group together till the date part using:
^([A-Za-z0-9-]{17})([-]{1})([A-Za-z0-9-]{15})([-]{1})
I intentionally kept the '-' separate so I could manipulate it later if I want to.
I am falling short when it comes to grouping the 'client name' and 'Paid' part of the format.
Would really appreciate any help grouping the last two parts.
Thanks a lot!
答案1
得分: 0
尝试这个:
^([a-zA-Z\d-]+)(-)(Date-\d{4}\d{2}-\d{2})(-)(Client-[a-zA-Z\d-]+)(-)([a-zA-Z\d])+$
这匹配:
- 任何由字母、数字或连字符组成的字符串
- 后跟连字符
- 后跟
Date-
和日期 - 后跟连字符
- 后跟
Client
和由字母、数字或连字符组成的任何字符串 - 后跟连字符
- 后跟单个单词
英文:
Try this:
^([a-zA-Z\d-]+)(-)(Date-\d{4}\d{2}-\d{2})(-)(Client-[a-zA-Z\d-]+)(-)([a-zA-Z\d])+$
This matches
- any string of letters, numbers, or hyphens
- followed by a hyphen
- followed by
Date-
and a date - followed by a hyphen
- followed by
Client
and any string of letters, number, or hyphens - followed by a hyphen
- followed by a single word
答案2
得分: 0
你可以尝试使用正向否定预查来匹配:
(?<=-\d{2}-)(Date-\d{4}-\d{2}-\d{2})-(.*)(?=-Paid)
然后用位置相反的分组\2-\1
来替换匹配项(Python风格)。
正则表达式解释:
(?<=-\d{2}-)
:正向后查(匹配项之前的内容)-\d{2}-
:破折号后跟两个数字和另一个破折号
(Date-\d{4}-\d{2}-\d{2})
:第1组,包含您的日期-
:破折号(.*)
:第2组,包含您的字符串(?=-Paid)
:正向前查(匹配项之后的内容)-Paid
:破折号后跟“Paid”
请在此处查看正则表达式演示。
英文:
You can try matching using lookarounds:
(?<=-\d{2}-)(Date-\d{4}-\d{2}-\d{2})-(.*)(?=-Paid)
Then replace the match with the groups inverted in position \2-\1
(Python flavor).
Regex Explanation:
(?<=-\d{2}-)
: positive lookbehind (what's before the match)-\d{2}-
: dash followed by two digits and another dash
(Date-\d{4}-\d{2}-\d{2})
: Group 1, containing your date-
: dash(.*)
: Group 2, containing your string(?=-Paid)
: positive lookahead (what's after the match)-Paid
: dash followed by "Paid"
Check the regex demo here.
答案3
得分: 0
请注意,以下是您要翻译的内容:
Group 1 = 公司名称_发票_# 例如 "CompanyName-Invoice-99"
Group 2 = 日期 例如 "2023-12-31"
Group 3 = 客户_blah "Client-Boombastic-Fantastic"
新 = ()-()-日期-()-已支付
英文:
Be very specific if you can:
/(CompanyName\-Invoice-[0-9]{2})-(Client-[-A-Za-z]{2,})-Date-([0-9]{4}-[0-9]{2}-[0-9]{2})-Paid/
Group 1 = company_name_invoice_# e.g. "CompanyName-Invoice-99"
Group 2 = date e.g. "2023-12-31"
Group 3 = client_blah "Client-Boombastic-Fantastic"
new = ()-()-Date-()-Paid
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论