匹配带有可选字符的字符串

3
我想从下面的URL中捕获'com.ouest.france'。我已经尝试使用以下正则表达式来实现。
play.google.com\/store\/apps\/details\?id=(.*)[?&]

点击此处查看:http://www.rubular.com/r/NMxRo4grSL

https://play.google.com/store/apps/details?id=com.ouest.france&hl=de https://play.google.com/store/apps/details?id=com.ouest.france

然而,如果没有“&”符号,则无法正常工作。

有什么想法吗?


你使用的是 Ruby 吗? - Wiktor Stribiżew
实际上是Java,但应该非常相似。 - Florent Valdelievre
1
你可以尝试使用 play.google.com\/store\/apps\/details\?id=([^&]*).* - Jose Ricardo Bustos M.
我认为你需要使用java.net.*中的URL来解析一个URL。 - Wiktor Stribiżew
2个回答

1

0
问题是你的正则表达式在末尾期望一个 ? 或者 &。将它设为可选项,像这样
play.google.com\/store\/apps\/details\?id=(.*?)([?&]|$)

该编程语言支持使用?&或者行末符号。

祝好!


但是为什么查询字符串中会出现另一个“?”呢? - user663031
@torazaburo 那个 ? 被引用并匹配了 URL 中的 ? - SamWhan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接