有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Matcher。group()正在返回预期结果的一部分。对于前一个url“www.google.com”,我的matcher返回“www”

我的要求是使用正则表达式检查字符串中的URL。 我所做的是使用模式和匹配器查找字符串是否包含URL

 val pattern = Pattern.compile(HyperlinkParser.validRegex.toString())
    val matcher = pattern.matcher(htmlParsedMessage) //"abcd www.google.com def"
    while (matcher.find()) {
        val url = matcher.group()//contains the required url but it returns "www.".Expected "www.google.com"
        val indicesPair = Pair(matcher.start(), matcher.end())
        hyperlinkStartEndIndicesList.add(indicesPair)
    }
    matcher.reset()

其中HyperlinkParser.validRegex

private const val regularExpression = "(?:(?:https?|ftp|file):|www.|ftp.)(?:([-A-Z0-9+&@#/%=~_|\$?!:,.]*)|[-A-Z0-9+&@#/%=~_|\$?!:,.])*(?:([-A-Z0-9+&@#/%=~_|\$?!:,.]*)|[A-Z0-9+&@#/%=~_|\$])"
val validRegex = Regex(regularExpression,RegexOption.IGNORE_CASE)

我期待着URL“www.google.com”,但它返回“www.”

你知道问题出在哪里吗。任何帮助都会被极大地接受


共 (1) 个答案

  1. # 1 楼答案

    关于^{}^{}方法的文档

    Returns the string representation of this regular expression, namely the pattern of this regular expression.

    Note that another regular expression constructed from the same pattern string may have different options and may match strings differently.

    这意味着它与不带IGNORE_CASE选项的regularExpressionstring相同

    因此,当您执行val pattern = Pattern.compile(HyperlinkParser.validRegex.toString())时,您会丢失不区分大小写的选项,这就是为什么google.com不匹配,因为您的正则表达式只匹配A-Z

    将该行更改为:

    val pattern = HyperlinkParser.validRegex.toPattern()
    

    这是可行的,因为toPattern文档说:

    Returns an instance of Pattern with the same pattern string and options as this instance of Regex has.

    Provides the way to use Regex where Pattern is required.