在Golang中,根据字符串模式的条件匹配,将正则表达式与相同的组匹配

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

Match a RegEx to the same Group based on a conditional match of a string pattern- in Golang

问题

我有两个测试字符串:

mp4/dach/zdf/22/07/220724_traumorte_italiens_romantische_inseln_inf/1/220724_traumorte_italiens_romantische_inseln_inf_508k_p9v15.mp4

/mp4/none/zdf/21/11/211124_1925_sendung_ban/1/211124_1925_sendung_ban_a3a4_508k_p9v15.mp4

我想要一个单一的正则表达式来匹配第一个字符串,并得到输出:

/dach/zdf/22/07/220724_traumorte_italiens_romantische_inseln_inf/1/220724_traumorte_italiens_romantische_inseln_inf

我可以使用正则表达式 ^.*[mp][4](.+)(?:a[0-9]a[0-9])?(?:.+_.+_.+_|_.+_).*$^.*[mp][4](.+)(a[0-9]a[0-9]?(.+_.+_.+_)|(_.+_)).*$ 来实现这个目的。

但是这个正则表达式也会将第二个字符串匹配为 /none/zdf/21/11/211124_1925_sendung_ban/1/211124_1925_sendung_ban_a3a4

而我的期望是,如果存在 a3a4,则匹配 3_;否则匹配 2_。这样,第二个字符串应该匹配为 /none/zdf/21/11/211124_1925_sendung_ban/1/211124_1925_sendung_ban

但是,不知何故,这个正则表达式没有按照预期进行分组(Group -1)。

如果有任何指导,我将不胜感激。

谢谢!

英文:

I have two test strings
mp4/dach/zdf/22/07/220724_traumorte_italiens_romantische_inseln_inf/1/220724_traumorte_italiens_romantische_inseln_inf_508k_p9v15.mp4

and

/mp4/none/zdf/21/11/211124_1925_sendung_ban/1/211124_1925_sendung_ban_a3a4_508k_p9v15.mp4

I want a single RegEx to match the first string to get an output
/dach/zdf/22/07/220724_traumorte_italiens_romantische_inseln_inf/1/220724_traumorte_italiens_romantische_inseln_inf

which I get with the RegEx ^.*[mp][4](.+)(?:a[0-9]a[0-9])?(?:.+_.+_.+_|_.+_).*$
or
^.*[mp][4](.+)(a[0-9]a[0-9]?(.+_.+_.+_)|(_.+_)).*$

but this matches the second string as /none/zdf/21/11/211124_1925_sendung_ban/1/211124_1925_sendung_ban_a3a4

but my expectation is to match 3 times _ if a3a4 is present; otherwise 2 times _
which should get the match /none/zdf/21/11/211124_1925_sendung_ban/1/211124_1925_sendung_ban for the second string.

But, somehow this is not grouping (Group -1) as intended.

Any pointers on this is appreciated.

Thanks!

答案1

得分: 1

你可以使用以下正则表达式进行匹配:

^\S*?\bmp4(/\S*?)(?:_a[0-9]a[0-9])?(?:_[^\s_]*){2}\.mp4$

解释

  • ^ 字符串的开头
  • \S*?\bmp4 匹配可选的非空白字符,尽可能少地匹配,然后匹配 mp4
  • ( 捕获组 1
    • /\S*? 匹配 /,然后匹配可选的非空白字符,尽可能少地匹配
  • ) 结束 捕获组 1
  • (?:_a[0-9]a[0-9])? 可选地匹配非捕获组中的模式
  • (?:_[^\s_]*){2} 重复两次 _ 和除了 _ 和空白字符之外的任意字符
  • \.mp4 匹配 .mp4
  • $ 字符串的结尾

可以在 regex demo 中查看演示。

英文:

You could use:

^\S*?\bmp4(/\S*?)(?:_a[0-9]a[0-9])?(?:_[^\s_]*){2}\.mp4$

Explanation

  • ^ Start of string
  • \S*?\bmp4 Match optional non whitespace chars, as least as possible, and them match mp4
  • ( Capture group 1
    • /\S*? Match / and match optional non whitespace chars, as least as possible
  • ) Close group 1
  • (?:_a[0-9]a[0-9])? Optionally match the pattern in the non capture group
  • (?:_[^\s_]*){2} Repeat 2 times _ and any char except _ and a whitespace char
  • \.mp4 Match .mp4
  • $ End of string

See a regex demo

huangapple
  • 本文由 发表于 2022年7月19日 05:11:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/73028684.html
匿名

发表评论

匿名网友

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

确定