你需要在 Java 中转义 * 字符吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14205498/
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-10-31 15:27:42  来源:igfitidea点击:

Do You Need To Escape * Character in Java?

javastringsplitescapingwildcard

提问by This 0ne Pr0grammer

So I wrote a little section of code in a program of mine that uses the split method to parse out and sort the individual components held within a string. Below is a sample of the code:

所以我在我的程序中编写了一小段代码,该程序使用 split 方法来解析和排序字符串中的各个组件。下面是代码示例:

String[] sArray;

String delimiter = "*";

String test = "TEXT*TEXT*TEXT*";
String a = "";
String b = "";
String c = "";

sArray= test.split(delimiter);

a = sArray[0];
b = sArray[1];
c = sArray[2];

System.out.println("A is: " + a + "\nB is: " + b + "\nC is: " + c);

However, when I run this program, it returns an error stating that:

但是,当我运行这个程序时,它返回一个错误,指出:

Dangling meta character '*' near index 0

Dangling meta character '*' near index 0

So I googled this error and found it might be related to an issue with how *can be considered a wildcard character in regex. So I tried:

所以我用谷歌搜索了这个错误,发现它可能与如何*在正则表达式中被视为通配符的问题有关。所以我试过:

String delimiter = "\*"

String delimiter = "\*"

But that just returned another error stating:

但这只是返回了另一个错误,指出:

illegal escape character

illegal escape character

Has anyone ran into this issue before or know how you are (if you are) supposed to escape *in java?

有没有人以前遇到过这个问题,或者知道你(如果你)应该如何*在 Java 中逃脱?

回答by jlordo

You also have to escape the backslash:

您还必须转义反斜杠:

String delimiter = "\*";

Because the string literal "\\"is just a single backslash, so the string literal "\\*"represents an escaped asterisk.

因为字符串文字"\\"只是一个反斜杠,所以字符串文字"\\*"代表一个转义的星号。

回答by Stephen C

The way to understand the solution is to realize that there are two "languages" in play here. You have the language of regular expressions (Java flavour) in which *is a meta-character that needs to be escaped with a \to represent a literal *. That gives you

理解解决方案的方法是意识到这里有两种“语言”在起作用。您拥有正则表达式语言(Java 风格),其中*需要用 a 转义的元字符\来表示文字*。那给你

''\*''    // not Java yet ...

But you are trying to represent that string in the "language" of Java String literals where a \is the escape character. So that requires that any literal \(in whatever it is we are trying to represent) must be escaped. This gives you

但是您试图用 Java 字符串文字的“语言”来表示该字符串,其中 a\是转义字符。所以这要求\必须转义任何文字(无论我们试图表示的是什么)。这给你

"\*"     // now Java ...

If you apply this approach/thinking consistently - "first do the regex escaping, then do the String literal escaping" - you won't get confused.

如果您始终如一地应用这种方法/思维——“首先进行正则表达式转义,然后进行字符串文字转义”——您就不会感到困惑。

回答by manisha mulchandani

You will have to attach "\" before "*" as if you want to represent "\" in java you have to use one more "\" before it.

你必须在“*”之前附加“\”,就像你想在java中代表“\”一样,你必须在它之前再使用一个“\”。

So, "\" represents:-----> "\" So if you want to use "" as delimiter the use "\"

所以,“\”代表:----->“\”所以如果你想使用“ ”作为分隔符使用“\

Corrected Code:

更正的代码:

String delimiter = "\*";
String test = "TEXT*TEXT*TEXT*";
String a = "";
String b = "";
String c = "";
sArray= test.split(delimiter);
a = sArray[0];
b = sArray[1];
c = sArray[2];
System.out.println("A is: " + a + "\nB is: " + b + "\nC is: " + c);

OUTPUT:

输出:

A is: TEXT

B is: TEXT

C is: TEXT

A 是:文本

B 是:文本

C是:文本