我在看一些jdk代码.我找到了这些角色.有人可以向我解释这些意味着什么.
public static String quote(String s) {
int slashEIndex = s.indexOf("\\E"); // What does this mean. Is this a special char in java. if so what does this do.
if (slashEIndex == -1)
return "\\Q" + s + "\\E";
StringBuilder sb = new StringBuilder(s.length() * 2);
sb.append("\\Q");
slashEIndex = 0;
int current = 0;
while ((slashEIndex = s.indexOf("\\E", current)) != -1) {
sb.append(s.substring(current, slashEIndex));
current = slashEIndex + 2;
sb.append("\\E\\\\E\\Q");
}
sb.append(s.substring(current, s.length()));
sb.append("\\E");
return sb.toString();
}
Run Code Online (Sandbox Code Playgroud)
从上面的代码示例中,我能够找出正在发生的事情,因为在方法中找到了\的出现并将它们转换为\ E和\ Q. …
我如何只提取匹配文本的子字符串.
我有一个包含多行的XML文件.然而,这就是我所关注的.
<url>/localhost/index.html</url>
Run Code Online (Sandbox Code Playgroud)
我试过了
cat file.txt | grep -o '<url>.*</url>'
Run Code Online (Sandbox Code Playgroud)
它给了我整条路线.我只想/localhost/index.html打印.有没有其他我可以使用的选项,就像我在Python中所知,您可以将正则表达式分组到子组中并选择要打印的那个.