1.基于此代码https://docs.oracle.com/javase/tutorial/essential/regex/test_harness.html
谁能解释一下
中的\"%s\"
是什么吗?
while (matcher.find()) {
console.format("I found the text" + " \"%s\" starting at " +
"index %d and ending at index %d.%n",
matcher.group(),
matcher.start(),
matcher.end());
found = true;
}
我只知道%s
代表字符串。
2.引用元字符https://docs.oracle.com/javase/tutorial/essential/regex/literals.html这个输出有什么解释吗?
最佳答案
\"%s\"
表示您想在两个引号之间放置一个字符串,例如结果可以是:
I found the text "some string"
//""-------------^ ^
// %s ^_________^
%s
用于字符串,如果您想使用数字,可以使用 %d
等等
在Java中引号应该被转义如何?你可以使用反斜杠\"
所以考虑我有一个包含像这样的引号的字符串Hello "World"
如何在 Java 中将其用作字符串:
String string = "Hello "World""; //<<------- this is an error syntax
要解决这个问题,您必须转义引号:
String string = "Hello \"World\"";
^ ^
看看https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html
关于java - 我正在尝试学习正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48463713/