关于特定文件名的正则表达式格式的查询

huangapple go评论53阅读模式
英文:

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:

(?&lt;=-\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:

  • (?&lt;=-\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. &quot;CompanyName-Invoice-99&quot;
Group 2 = date e.g. &quot;2023-12-31&quot;
Group 3 = client_blah &quot;Client-Boombastic-Fantastic&quot;


new = ()-()-Date-()-Paid

huangapple
  • 本文由 发表于 2023年6月2日 07:50:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386374.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定