正则表达式:如果它包含example.com/20,请选择整个链接

时间:2016-04-26 20:43:41

标签: java android regex

我在android中使用linkify,我想为包含example.com/20的链接触发我自己的活动,并激活其他链接的隐含意图。

例如,点击这些链接会触发我自己的活动。

    https://example.com/2013/04/blah-blah-balh.html
    http://example.com/2005/04/jurassic-pack-ola.html
    https://www.example.com/2012/07/tlion-film-nolly.html
    http://www.example.com/2016/08/rolj-ola-ola-po-me-them.html

这些链接应触发隐含意图

    http://www.example.com/rolj-ola-ola-po-me-them.html
    https://hopeman.com/2016/12/juujjhgg.html
    https://ekapurk.com/2001/04/tyhtt-poiuut-i.html

我在http://regexr.com/中对\bexample(.com\/{20})?\b进行了测试,但它只是高亮显示示例

2 个答案:

答案 0 :(得分:2)

您可能需要检查https?:\/\/(www\.)?example\.com\/20与example.com上的http和https网址匹配的网址,以及网址中以/20开头的www.example.com网址。

在这种情况下,你只需要在html标签中嵌入你的网址即可。

答案 1 :(得分:1)

您可以使用

String pattern = "\\bexample[.]com/20";

或者如果您需要匹配完整的字符串:

String pattern = "https?://(?:w{3}[.])?example[.]com/20\\S*";

请注意,\/{20}匹配20个斜杠,(.com\/{20})?是可选的,不必匹配,因为?表示 1或0次出现。此外,.可以匹配任何字符,但可以匹配换行符,它应该被转义或放在字符类中。

请参阅this regex demo 1regex demo 2

模式细节:

  • https? - 匹配httphttps(因为s?表示匹配1或0 s s)
  • :// - 文字序列://
  • (?:w{3}[.])? - 1或0(=可选)3个字母w及其后的文字点(www.
  • example[.]com/20 - 文字字符串example.com/20
  • \S* - 除空白外的零个或多个(*)个字符。