【发布时间】:2017-02-13 04:06:38
【问题描述】:
我有这些字符串;
wordsExpanded="test | is | [(thirty four) {<number_type_0 words>}( 3 4 ) {<number_type_0 digits>}] | test | [(three) {<number_type_1 words>}( 3 ) {<number_type_1 digits>}] | [(one) {<number_type_2 words>}( 1 ) {<number_type_2 digits>}]"
interpretation="{<number_type_2 digits> <number_type_1 digits> <number_type_0 words>}"
我需要的输出是这样的字符串;
finalOutput="test | is | thirty four | test | 3 | 1 "
基本上,解释字符串具有确定使用哪个组所需的信息。 对于第一个,我们使用,因此正确的字符串是“(三十四)”而不是“(3 4)” 第二个是“(3)”,然后是“(1)”
这是我目前的代码;
package com.test.prova;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Prova {
public static void main(String[] args) {
String nlInterpretation="{<number_type_2 digits> <number_type_1 digits> <number_type_0 words>}";
String inputText="this is 34 test 3 1";
String grammar="test is [(thirty four) {<number_type_0 words>}( 3 4 ) {<number_type_0 digits>}] test [(three) {<number_type_1 words>}( 3 ) {<number_type_1 digits>}] [(one) {<number_type_2 words>}( 1 ) {<number_type_2 digits>}]";
List<String> matchList = new ArrayList<String>();
Pattern regex = Pattern.compile("[^\\s\"'\\[]+|\\[([^\\]]*)\\]|'([^']*)'");
Matcher regexMatcher = regex.matcher(grammar);
while (regexMatcher.find()) {
if (regexMatcher.group(1) != null) {
matchList.add(regexMatcher.group(1));
} else if (regexMatcher.group(2) != null) {
matchList.add(regexMatcher.group(2));
} else {
matchList.add(regexMatcher.group());
}
}
String[] xx = matchList.toArray(new String[0]);
String[] yy = inputText.split(" ");
matchList = new ArrayList<String>();
regex = Pattern.compile("[^<]+|<([^>]*)>");
regexMatcher = regex.matcher(nlInterpretation);
while (regexMatcher.find()) {
if (regexMatcher.group(1) != null) {
matchList.add(regexMatcher.group(1));
}
}
String[] zz = matchList.toArray(new String[0]);
System.out.println(String.join(" | ",zz));
for (int i=0; i<xx.length; i++) {
if (xx[i].contains("number_type_")) {
matchList = new ArrayList<String>();
regex = Pattern.compile("[^\\(]+|<([^\\)]*)>.*[^<]+|<([^>]*)>");
regexMatcher = regex.matcher(xx[i]);
while (regexMatcher.find()) {
if (regexMatcher.group(1) != null) {
matchList.add(regexMatcher.group(1));
} else if (regexMatcher.group(2) != null) {
matchList.add(regexMatcher.group(2));
} else {
matchList.add(regexMatcher.group());
}
}
System.out.println(String.join(" | ",matchList.toArray(new String[0])));
}
System.out.printf("%02d\t%s\t->%s\n", i, yy[i], xx[i]);
}
}
}
生成的输出如下;
number_type_2 digits | number_type_1 digits | number_type_0 words
00 this ->test
01 is ->is
thirty four) {<number_type_0 words>} | 3 4 ) {<number_type_0 digits>}
02 34 ->(thirty four) {<number_type_0 words>}( 3 4 ) {<number_type_0 digits>}
03 test ->test
three) {<number_type_1 words>} | 3 ) {<number_type_1 digits>}
04 3 ->(three) {<number_type_1 words>}( 3 ) {<number_type_1 digits>}
one) {<number_type_2 words>} | 1 ) {<number_type_2 digits>}
05 1 ->(one) {<number_type_2 words>}( 1 ) {<number_type_2 digits>}
我想要的更像是这样的;
number_type_2 digits | number_type_1 digits | number_type_0 words
00 this ->test
01 is ->is
02 34 ->thirty four
03 test ->test
04 3 ->3
05 1 ->1
【问题讨论】:
-
你能举出第三个例子吗?
-
非常不清楚。举个例子
-
我不知道这个问题是什么意思。灰色部分是实际的字符串,还是元句法变量?
-
我希望这更清楚。
-
不,恐怕真的不是。