Java String split 没有返回正确的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5675704/
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
Java String split not returning the right values
提问by Richard Stokes
I'm trying to parse a txt file that represents a grammar to be used in a recursive descent parser. The txt file would look something like this:
我正在尝试解析一个 txt 文件,该文件表示要在递归下降解析器中使用的语法。txt 文件看起来像这样:
SPRIME ::= Expr eof
Expr ::= Term Expr'
Expr' ::= + Term Expr' | - Term Expr' | e
SPRIME ::= Expre of
Expr ::= Term Expr'
Expr' ::= + Term Expr' | - 期限 Expr' | 电子
To isolate the left hand side and split the right hand side into seperate production rules, I take each line and call:
为了隔离左侧并将右侧拆分为单独的产生式规则,我取每一行并调用:
String[] firstSplit = line.split("::=");
String LHS = firstSplit[0];
String productionRules = firstSplit[1].split("|");
However, when I call the second split method, I am not returned an array of the Strings separated by the "|" character, but an array of each indiviudual character on the right hand side, including "|". So for instance, if I was parsing the Expr' rule and printed the productionRules array, it would look like this:
但是,当我调用第二个 split 方法时,我没有返回由“|”分隔的字符串数组 字符,而是右侧每个单独字符的数组,包括“|”。因此,例如,如果我正在解析 Expr' 规则并打印 productionRules 数组,它将如下所示:
"+"
"Term"
"Expr'"
""
"|"
"+"
"Term"
"Expr'"
""
"|"
When what I really want should look like this:
当我真正想要的应该是这样的:
- Term Expr'
- 期限 Expr'
Anyone have any ideas what I'm doing wrong?
任何人都知道我做错了什么?
回答by Alnitak
The parameter to String.split()
is a regular expression, and the vertical bar character is special.
to 参数为String.split()
正则表达式,竖线字符特殊。
Try escaping it with a backslash:
尝试用反斜杠转义它:
String productionRules = firstSplit[1].split("\|");
NB: two backslashes are required, since the backslash character itself is special within string literals.
注意:需要两个反斜杠,因为反斜杠字符本身在字符串文字中是特殊的。
回答by dcn
Since split
takes a regex as argument you have to escape all non-intended regex symbols.
由于split
将正则表达式作为参数,您必须转义所有非预期的正则表达式符号。
回答by Prince John Wesley
You need to escape pipe(|
) symbol which is a regex
OR
operator .
你需要转义 pipe( |
) 符号,它是一个regex
OR
操作符。
String productionRules = firstSplit[1].split("\|");
or
或者
String productionRules = firstSplit[1].split(Pattern.quote("|"));
回答by Jon Bright
The pipe character is the regex operator for "or". What you want is
管道字符是“或”的正则表达式运算符。你想要的是
String productionRules = firstSplit[1].split("\|");
which tells it to look for an actual pipe character.
这告诉它寻找一个实际的管道字符。