Regex 从文本获取链接

Regex 从文本获取链接,regex,scala,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 t

所以我有一个简单的文本:

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))

您可以在正则表达式中使用多行修饰符
(?m)
,使
^
$
匹配行的开始和结束,而不是整个字符串:

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


由于您只需要一个URL,我建议使用
findFirstIn
而不是
findAllIn
(请参阅)。

这里是1:
^https.*$
您使用的是Scala代码还是正则表达式?请参阅以获取正则表达式。请参阅我的更新。该链接是否始终在一行上?然后只需将
(?m)
预先添加到您的模式中,然后重试。为什么要使用
findAllIn
?会有更多的链接吗?
str
包含所有的行吗?
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)