从文本获取链接

时间:2016-05-16 07:08:50

标签: regex scala

所以我有这个简单的文字:

To activate your account click the link below:
https://tbeyfee-gkg9834636j-gergity3yu3hgge-drgengo9476y3ltjne
If the above URL does not work try copying and pasting it into your browser. If you continue to have problem please feel free to contact us.
If you have any questions, please do not hesitate to contact your account manager directly or email us at info@logger.com and we'll get right back to you.
Thanks again for choosing logger.
Kind regards,
The Logger Team

捕获此https链接的简单方法是什么?

这就是我的尝试:

val str = "" // This is my string.
val pattern = new Regex("^https.*$")

println(pattern.findAllIn(str))

1 个答案:

答案 0 :(得分:2)

您可以将多线修饰符(?m)与正则表达式一起使用,使^$匹配行的开头和结尾而不是整个字符串

var str = "To activate your account click the link below:\nhttps://tbeyfee-gkg9834636j-gergity3yu3hgge-drgengo9476y3ltjne\nIf the above URL does not work try copying and pasting it into your browser. If you continue to have problem please feel free to contact us.\nIf you have any questions, please do not hesitate to contact your account manager directly or email us at info@logger.com and we'll get right back to you.\nThanks again for choosing logger.\nKind regards,\nThe Logger Team"
val pattern = new Regex("(?m)^https://.+$")
val res = pattern.findFirstIn(str)
println(res)

请参阅Ideone demo

我还建议用*替换+(0次或更多次出现)量词,以匹配1个或多个出现的任何字符,但换行符. })。此外,您可以使用https?://\S+来匹配较大文本中的大多数网址。

由于您只需要1个网址,我建议您使用findFirstIn代替findAllIn(请参阅Scala Regex reference)。