[英]Java String split not returning the right values
I'm trying to parse a txt file that represents a grammar to be used in a recursive descent parser. 我正在尝试解析一个表示要在递归下降解析器中使用的语法的txt文件。 The txt file would look something like this:
txt文件看起来像这样:
SPRIME ::= Expr eof SPRIME :: = Expr eof
Expr ::= Term Expr' Expr :: = Term Expr'
Expr' ::= + Term Expr' | Expr':: = + Term Expr'| - Term Expr' |
- Term Expr'| e
Ë
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 "|" 但是,当我调用第二个split方法时,我没有返回由“|”分隔的字符串数组 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:
例如,如果我正在解析Expr'规则并打印了productionRules数组,它将如下所示:
"+" “+”
"Term" “术语”
"Expr'" “Expr的'”
"" “”
"|" “|”
When what I really want should look like this: 当我真正想要的应该是这样的:
Anyone have any ideas what I'm doing wrong? 任何人都有任何想法,我做错了什么?
The parameter to String.split()
is a regular expression , and the vertical bar character is special. 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. 注意:需要两个反斜杠,因为反斜杠字符本身在字符串文字中是特殊的。
由于split
采用正则表达式作为参数,因此必须转义所有非预期的正则表达式符号。
You need to escape pipe( |
) symbol which is a regex
OR
operator . 您需要转义管道(
|
)符号,它是一个regex
OR
运算符。
String productionRules = firstSplit[1].split("\\|");
or 要么
String productionRules = firstSplit[1].split(Pattern.quote("|"));
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. 它告诉它寻找一个实际的管道角色。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.