java 如何用"替换双引号 使用 replaceAll 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34214364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:36:49  来源:igfitidea点击:

How to replace double quote with " using replaceAll method

javaregex

提问by libardo

I've seen other people ask similar questions to this before and have followed the instructions given to those people, but I still cannot get my code to function correctly.

我以前见过其他人问过类似的问题,并按照给这些人的说明进行了操作,但我仍然无法使我的代码正常运行。

try {

    FileReader fr_p = new FileReader("p.txt");
    BufferedReader in_p = new BufferedReader(fr_p);

    String line = in_p.readLine();

    for (;;) {

        line = line.replaceAll("&","&");
        line = line.replaceAll("<","&lt;");
        line = line.replaceAll(">","&gt;");
        line = line.replaceAll("\"","&quot;");

        people.add(line);
        line = in_p.readLine();
        if (line == null) break;

    }

    in_p.close();
} catch (FileNotFoundException e) {

    System.out.println("File p.txt not found.");
    System.exit(0);

} catch (IOException e) {

    System.out.println("Error reading from file.");
    System.exit(0);

}

This is the code that I've written to attempt to take each name on a separate line of a text file, and put it into an ArrayList, replacing the special characters with their XML entities. I then write this into an HTML file later on.

这是我编写的代码,试图将每个名称放在文本文件的单独行中,并将其放入 ArrayList,用它们的 XML 实体替换特殊字符。稍后我将其写入 HTML 文件。

The code that I've written does this just fine for the first three characters, but when it reaches the line attempting to change any double quotes to &quot;, it doesn't change them and ends up giving me ainstead of a double quote. I'm not sure what else I should change about my code to get this to work.

我编写的代码对前三个字符执行得很好,但是当它到达尝试将任何双引号更改为 的行时&quot;,它不会更改它们并最终给我a而不是双引号。我不确定我还应该对代码进行哪些更改才能使其正常工作。

回答by Peter Lawrey

When I run

当我跑

String line = "This is a string with \" and \" in it";
line = line.replaceAll("\"","&quot;");
System.out.println(line);

I get

我得到

This is a string with &quot; and &quot; in it

Note: there is lots of different kinds of quotation marks but there is only one "character. If you have a different quotation mark, it will not match.

注意:引号有很多种,但只有一个"字符。如果您有不同的引号,它将不匹配。

https://en.wikipedia.org/wiki/Quotation_mark

https://en.wikipedia.org/wiki/Quotation_mark

回答by Autumn

I got the same behaviour as you. The java compiler is eating your escape character on the quote'"' character. There must be something odd with regex compiler expecting a quote to be escaped as well when fed a string literal. It isn't supposed to, but in this case it is.

我有和你一样的行为。java编译器在引号'"'字符上吃掉你的转义字符。正则表达式编译器肯定有一些奇怪的东西,当输入字符串文字时,它期望引号也被转义。它不应该,但在这种情况下它是。

If you prepend an escaped escape, it will work.

如果你预先准备一个逃逸的逃逸,它会起作用。

   String lineout = line.replaceAll("\\"","&quote;");

Alternately, you can can use a String object for your search expression.

或者,您可以将 String 对象用于搜索表达式。

   String line = "embedded\"here";
   String searchstring = "\"";
   String lineout = line.replaceAll(searchstring,"&quote;");

回答by Gonzalo

I would change your code to something like this

我会把你的代码改成这样

line = line.replace("&","&amp;")
           .replace("<","&lt;")
           .replace(">","&gt;")
           .replace("\"","&quot;");

It should work like yours but there is no need to use regexp for simply replacement.

它应该像您一样工作,但无需使用正则表达式进行简单替换。

回答by Maouven

you have an encoding problem, you can resolve that by setting the single quote by its unicode code :

您有编码问题,您可以通过其 unicode 代码设置单引号来解决该问题:

line = line.replaceAll("\"", "\u0027");

回答by Yash ajabiya

Replace with

用。。。来代替

String replace = line.replace("&quot;", "''");