根据关键字搜索句子并返回与关键字匹配的单词

时间:2020-08-18 06:29:28

标签: android regex string kotlin

我需要获取某个公司的URL链接,然后对该URL进行一些处理。 目前,我正在尝试使用PatternMatcher来获得单词,但没有达到我的要求。

val p: Pattern = Pattern.compile("abc.com")
val m: Matcher = p.matcher("Hi, welcome to ABC Website. test-page.abc.com/?12345 link to your profile.")
if (m.find()) {
    AppLog.d("UnitTest", "Found : ${m.group()}")
} else {
    AppLog.d("UnitTest", "Not found")
}

运行这个,我得到这个输出

发现:abc.com

我有办法代替整个链接吗?可以使用任何正则表达式来让我得到这个吗?

找到了:test-page.abc.com/?12345

1 个答案:

答案 0 :(得分:1)

根据我的评论,您可以考虑以下内容:

2020-09

这将提取一个包含\S*abc\.com\S* 但包含除空格字符之外的所有前导和尾随字符的子字符串。

相关问题