提取两组字符之间的文本,其中最后一组是可选的。

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

Regex: Extract text between 2 group of characters where the last group is optional

问题

我在努力创建一个正则表达式,能够提取在两组字符之间的数据,其中最后一组字符不会出现在所有匹配项中。

我想要获取以下数据:

important-info-1
otherimportant-info-3
moreimportantinfo
otherimportant-info-4
英文:

I'm struggling to create a regex capable of extract data between 2 group of characters where the last group of characters don't appear in all the matches.

I would like to obtain the following data

https://www.myurl.com/tag/important-info-1/page/34/
https://www.myurl.com/tag/otherimportant-info-3
https://www.myurl.com/tag/moreimportantinfo/page/1
https://www.myurl.com/tag/otherimportant-info-4/

and the result

important-info-1
otherimportant-info-3
moreimportantinfo
otherimportant-info-4

答案1

得分: 0

?-运算符在这里非常有用,因为它表示表达式是可选的:

正则表达式

https://www\.myurl\.com/tag/([^/]+)(second_group)?

匹配包含第二组的字符串以及不包含第二组的字符串。

英文:

The ?-operator is your friend here, as it signals that an expression is optional:

The regex

https://www\.myurl\.com/tag/([^/]+)(second_group)?

matches strings containing the second group as well as those without it.

huangapple
  • 本文由 发表于 2023年6月5日 23:06:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407763.html
匿名

发表评论

匿名网友

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

确定