英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论