使用Regex.fromLiteral()创建的Regex到底与什么匹配? [英] What exactly does a Regex created with Regex.fromLiteral() match?

查看:83
本文介绍了使用Regex.fromLiteral()创建的Regex到底与什么匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Regex.fromLiteral(".*")创建了一个非常简单的匹配所有正则表达式.

根据

但是我并没有真正理解对于指定的文字字符串"的含义.

请考虑以下示例:

fun main(args: Array<String>) {
    val regex1 = ".*".toRegex()
    val regex2 = Regex.fromLiteral(".*")
    println("regex1 matches abc: " + regex1.matches("abc"))
    println("regex2 matches abc: " + regex2.matches("abc"))
    println("regex2 matches .* : " + regex2.matches(".*"))  
}

输出:

regex1 matches abc: true
regex2 matches abc: false
regex2 matches .* : true

显然(与我的预期相反),Regex.fromLiteral()String.toRegex()的行为完全不同(我对regex2.matches()尝试了数十种不同的参数-唯一返回true的参数是.*)

这是否意味着使用Regex.fromLiteral()创建的正则表达式始终仅匹配使用其创建的精确字符串?

如果是,那么这种Regex有哪些可能的用例? (我想不出任何有用的方案)

解决方案

是的,它确实创建了与String中的文字字符匹配的正则表达式.当您尝试匹配将在正则表达式中解释的符号时,这非常方便-您不必以这种方式进行转义.

例如,如果要查找包含.*[](1)?[2]的字符串,则可以执行以下操作:

val regex = Regex.fromLiteral(".*[](1)?[2]")

regex.containsMatchIn("foo")                  // false
regex.containsMatchIn("abc.*[](1)?[2]abc")    // true

当然,使用常规的String方法,您几乎可以完成Regex的所有工作.

val literal = ".*[](1)?[2]"
literal == "foo"                       // equality checks
literal in "abc.*[](1)?[2]abc"         // containment checks
"some string".replace(literal, "new")  // replacements

但是有时您需要一个Regex实例作为参数,因此在这些情况下可以使用fromLiteral方法.在某些用例中,这些针对不同输入的不同操作的性能也可能很有趣.

I've created a very simple match-all Regex with Regex.fromLiteral(".*").

According to the documentation: "Returns a literal regex for the specified literal string."

But I don't really get what "for the specified literal string" is supposed to mean.

Consider this example:

fun main(args: Array<String>) {
    val regex1 = ".*".toRegex()
    val regex2 = Regex.fromLiteral(".*")
    println("regex1 matches abc: " + regex1.matches("abc"))
    println("regex2 matches abc: " + regex2.matches("abc"))
    println("regex2 matches .* : " + regex2.matches(".*"))  
}

Output:

regex1 matches abc: true
regex2 matches abc: false
regex2 matches .* : true

so apparently (and contrary to my expectations), Regex.fromLiteral() and String.toRegex() behave completely different (I've tried dozens of different arguments to regex2.matches() - the only one that returned true was .*)

Does this mean that a Regex created with Regex.fromLiteral() always matches only the exact string it was created with?

If yes, what are possible use cases for such a Regex? (I can't think of any scenario where that would be useful)

解决方案

Yes, it does indeed create a regex that matches the literal characters in the String. This is handy when you're trying to match symbols that would be interpreted in a regex - you don't have to escape them this way.

For example, if you're looking for strings that contain .*[](1)?[2], you could do the following:

val regex = Regex.fromLiteral(".*[](1)?[2]")

regex.containsMatchIn("foo")                  // false
regex.containsMatchIn("abc.*[](1)?[2]abc")    // true

Of course you can do almost anything you can do with a Regex with just regular String methods too.

val literal = ".*[](1)?[2]"
literal == "foo"                       // equality checks
literal in "abc.*[](1)?[2]abc"         // containment checks
"some string".replace(literal, "new")  // replacements

But sometimes you need a Regex instance as a parameter, so the fromLiteral method can be used in those cases. Performance of these different operations for different inputs could also be interesting for some use cases.

这篇关于使用Regex.fromLiteral()创建的Regex到底与什么匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆