使用Regex.fromLiteral()创建的Regex到底与什么匹配? [英] What exactly does a Regex created with Regex.fromLiteral() match?
问题描述
我用Regex.fromLiteral(".*")
创建了一个非常简单的匹配所有正则表达式.
根据
但是我并没有真正理解对于指定的文字字符串"的含义. 请考虑以下示例: 输出: 显然(与我的预期相反), 这是否意味着使用 如果是,那么这种Regex有哪些可能的用例? (我想不出任何有用的方案) 是的,它确实创建了与 例如,如果要查找包含 当然,使用常规的 但是有时您需要一个 I've created a very simple match-all Regex with 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: Output: so apparently (and contrary to my expectations), Does this mean that a Regex 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 For example, if you're looking for strings that contain Of course you can do almost anything you can do with a But sometimes you need a 这篇关于使用Regex.fromLiteral()创建的Regex到底与什么匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!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()
创建的正则表达式始终仅匹配使用其创建的精确字符串? 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
方法.在某些用例中,这些针对不同输入的不同操作的性能也可能很有趣.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()
and String.toRegex()
behave completely different (I've tried dozens of different arguments to regex2.matches()
- the only one that returned true was .*
)Regex.fromLiteral()
always matches only the exact string it was created with? 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..*[](1)?[2]
, you could do the following:val regex = Regex.fromLiteral(".*[](1)?[2]")
regex.containsMatchIn("foo") // false
regex.containsMatchIn("abc.*[](1)?[2]abc") // true
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
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.