Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何伪装逃逸角色?\";一串_Java_Html_String_Escaping - Fatal编程技术网

Java 如何伪装逃逸角色?\";一串

Java 如何伪装逃逸角色?\";一串,java,html,string,escaping,Java,Html,String,Escaping,我在使用我制作的90%完整的语法荧光笔时遇到了一些困难。它所做的是从.java文件的源读取文本,检测关键字、注释等,并在HTML文件中写入(彩色)输出。它的样本输出是: (我无法上传整个html页面,因此这是一个屏幕截图。)(我希望)您可以看到,我的程序似乎可以正确地使用关键字、文字和注释(见下文),因此通常可以记录几乎所有程序。但是当我将“的转义序列(即\”存储在字符串中时,它似乎会崩溃。错误案例如下所示: 字符串文字高亮显示不会在文字的末尾停止,而是一直持续到找到另一个提示,如关键字或另

我在使用我制作的90%完整的语法荧光笔时遇到了一些困难。它所做的是从
.java
文件的源读取文本,检测关键字、注释等,并在
HTML
文件中写入(彩色)输出。它的样本输出是:

(我无法上传整个html页面,因此这是一个屏幕截图。)(我希望)您可以看到,我的程序似乎可以正确地使用
关键字
文字
注释
(见下文),因此通常可以记录几乎所有程序。但是当我将
的转义序列(即
\”
存储在
字符串中时,它似乎会崩溃。错误案例如下所示:

字符串文字高亮显示不会在文字的末尾停止,而是一直持续到找到另一个提示,如关键字或另一个文字

那么,问题是如何在
字符串中伪装/隐藏/删除此
\“

我的程序的
stringFilter
方法是:

public String stringFilter(String line) {
    if (line == null || line.equals("")) {
        return "";
    }
    StringBuffer buf = new StringBuffer();
    if (line.indexOf("\"") <= -1) {
        return keywordFilter(line);
    }
    int start = 0;
    int startStringIndex = -1;
    int endStringIndex = -1;
    int tempIndex;
    //Keep moving through String characters until we want to stop...
    while ((tempIndex = line.indexOf("\"")) > -1 && !isInsideString(line, tempIndex)) {
        //We found the beginning of a string
        if (startStringIndex == -1) {
            startStringIndex = 0;
            buf.append( stringFilter(line.substring(start,tempIndex)) );
            buf.append("</font>");
            buf.append(literal).append("\"");
            line = line.substring(tempIndex+1);
        }
        //Must be at the end
        else {
            startStringIndex = -1;
            endStringIndex = tempIndex;
            buf.append(line.substring(0,endStringIndex+1));
            buf.append("</font>");
            buf.append(normal);
            line = line.substring(endStringIndex+1);
        }
    }

    buf.append( keywordFilter(line) );

    return buf.toString();
}

使用双斜杠“\\”而不是“\”……也许它是有效的…

< p>使用双斜杠“\”而不是“\”……也许它是有效的…

< p>检查在TimeTimeX-1中找到的字符,它不考虑字符串的开始或结束。
String originalLine=line;
if ((tempIndex = originalLine.indexOf("\"", tempIndex + 1)) > -1) {
            if (tempIndex==0 || originalLine.charAt(tempIndex - 1) != '\\') { 
...

在TimeTimeX-1中查找CHAR,它不考虑字符串的开始或结束。

String originalLine=line;
if ((tempIndex = originalLine.indexOf("\"", tempIndex + 1)) > -1) {
            if (tempIndex==0 || originalLine.charAt(tempIndex - 1) != '\\') { 
...
应采取的步骤:

  • 首先将所有\“替换为一些临时字符串,如

    String tempStr="forward_slash_followed_by_double_quote";
    line = line.replaceAll("\\\\\"", tempStr);
    //line = line.replaceAll("\\\"", tempStr);
    
  • 做你正在做的事
  • 最后将该临时字符串替换为\“

要遵循的步骤:

  • 首先将所有\“替换为一些临时字符串,如

    String tempStr="forward_slash_followed_by_double_quote";
    line = line.replaceAll("\\\\\"", tempStr);
    //line = line.replaceAll("\\\"", tempStr);
    
  • 做你正在做的事
  • 最后将该临时字符串替换为\“


查找引号然后尝试确定是否转义的困难在于,仅仅查看前一个字符是否是反斜杠是不够的-请考虑

String basedir = "C:\\Users\\";
其中,
\“
不是转义引号,而是转义反斜杠,后跟未转义引号。通常,前面有奇数个反斜杠的引号是转义的,前面有偶数个反斜杠的引号不是

一种更合理的方法是从左到右一次解析字符串中的一个字符,而不是尝试跳转到引用字符。如果您不想学习像JavaCC或antlr这样的合适的解析器生成器,那么可以使用
\G
锚点使用正则表达式来解决这种情况(强制每个后续匹配在前一个匹配的末尾开始,没有间隙)-如果我们假设
str
是输入的子字符串,以字符串文字开头引号后的字符开始,则

Pattern p = Pattern.compile("\\G(?:\\\\u[0-9A-Fa-f]{4}|\\\\.|[^\"\\\\])");
StringBuilder buf = new StringBuilder();
Matcher m = p.matcher(str);
while(m.find()) buf.append(m.group());

将保留
buf
,其中包含字符串文字的内容,但不包括结束引号,并将处理转义,如
\“
\
和unicode转义
\unnn

查找引号然后尝试确定其是否转义的困难在于,仅查看前一个字符是否为反斜杠是不够的-请考虑

String basedir = "C:\\Users\\";
其中,
\“
不是转义引号,而是一个转义反斜杠,后跟一个未转义引号。通常,前面有奇数个反斜杠的引号是转义的,前面有偶数个反斜杠的引号不是

一种更明智的方法是从左到右一次解析字符串一个字符,而不是尝试跳转到前面引用字符。如果您不想学习像JavaCC或antlr这样的合适的解析器生成器,那么您可以使用
\G
锚定函数使用正则表达式来处理这种情况(强制每个后续匹配从上一个匹配的末尾开始,没有间隙)-如果我们假设
str
是输入的子字符串,从字符串文字的开头引号后面的字符开始,那么

Pattern p = Pattern.compile("\\G(?:\\\\u[0-9A-Fa-f]{4}|\\\\.|[^\"\\\\])");
StringBuilder buf = new StringBuilder();
Matcher m = p.matcher(str);
while(m.find()) buf.append(m.group());

将保留
buf
,其中包含字符串文字的内容,但不包括结束引号,并将处理转义,如
\“
\
和unicode转义
\unnn

我的想法是,当遇到反斜杠时,忽略下一个字符

String str = "blah\"blah\\blah\n";

int index = 0;
while (true) {
    // find the beginning
    while (index < str.length() && str.charAt(index) != '\"')
        index++;
    int beginIndex = index;
    if (index == str.length()) // no string found
        break;
    index++;
    // find the ending
    while (index < str.length()) {
        if (str.charAt(index) == '\\') {
            // escape, ignore the next character
            index += 2;
        } else if (str.charAt(index) == '\"') {
            // end of string found
            System.out.println(beginIndex + " " + index);
            break;
        } else {
            // plain content
            index++;
        }
    }
    if (index >= str.length())
        throw new IllegalArgumentException(
                "String literal is not properly closed by a double-quote");
    index++;
}
String str=“blah\”blah\\blah\n”;
int指数=0;
while(true){
//找到开始
而(索引=str.length())
抛出新的IllegalArgumentException(
“字符串文字未被双引号正确关闭”);
索引++;
}

我的想法是,当遇到反斜杠时,忽略下一个字符

String str = "blah\"blah\\blah\n";

int index = 0;
while (true) {
    // find the beginning
    while (index < str.length() && str.charAt(index) != '\"')
        index++;
    int beginIndex = index;
    if (index == str.length()) // no string found
        break;
    index++;
    // find the ending
    while (index < str.length()) {
        if (str.charAt(index) == '\\') {
            // escape, ignore the next character
            index += 2;
        } else if (str.charAt(index) == '\"') {
            // end of string found
            System.out.println(beginIndex + " " + index);
            break;
        } else {
            // plain content
            index++;
        }
    }
    if (index >= str.length())
        throw new IllegalArgumentException(
                "String literal is not properly closed by a double-quote");
    index++;
}
String str=“blah\”blah\\blah\n”;
int指数=0;
while(true){
//找到开始
而(索引