【发布时间】:2016-09-21 01:10:08
【问题描述】:
我正在尝试匹配所有包含符号 < 或 > 的令牌,但存在一些冲突。特别是,我的标记是<、>、</、/>,以及以<!-- 开头并以--> 结尾的评论。
我的正则表达式如下:
String LTHAN = "<";
String GTHAN = ">";
String LTHAN_SLASH = "</";
String GTHAN_SLASH = "/>";
String COMMENT = "<!--.*-->";
然后我通过使用通用方法将它们添加到列表中来编译它们:
public void add(String regex, int token) {
tokenInfos.add(new TokenInfo(Pattern.compile("^(" + regex + ")"), token));
}
这是我的 TokenInfo 类的样子:
private class TokenInfo {
public final Pattern regex;
public final int token;
public TokenInfo(Pattern regex, int token) {
super();
this.regex = regex;
this.token = token;
}
}
我匹配并显示列表如下:
public void tokenize(String str) {
String s = new String(str);
tokens.clear();
while (!s.equals("")) {
boolean match = false;
for (TokenInfo info : tokenInfos) {
Matcher m = info.regex.matcher(s);
if (m.find()) {
match = true;
String tok = m.group().trim();
tokens.add(new Token(info.token, tok));
s = m.replaceFirst("");
break;
}
}
}
}
读取和显示:
try {
BufferedReader br;
String curLine;
String EOF = null;
Scanner scan = new Scanner(System.in);
StringBuilder sb = new StringBuilder();
try {
File dir = new File("C:\\Users\\Me\\Documents\\input files\\example.xml");
br = new BufferedReader(new FileReader(dir));
while ((curLine = br.readLine()) != EOF) {
sb.append(curLine);
// System.out.println(curLine);
}
br.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
tokenizer.tokenize(sb.toString());
for (Tokenizer.Token tok : tokenizer.getTokens()) {
System.out.println("" + tok.token + " " + tok.sequence);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
示例输入:
<!-- Sample input file with incomplete recipe -->
<recipe name="bread" prep_time="5 mins" cook_time="3 hours">
<title>Basic bread</title>
<ingredient amount="3" unit="cups">Flour</ingredient>
<instructions>
<step>Mix all ingredients together.</step>
</instructions>
</recipe>
但是,输出的令牌列表将< 和/(包括后面的任何字符)识别为单独的令牌,这意味着它似乎永远无法识别令牌</ 和/>。 cmets也有同样的问题。这是我的正则表达式的问题吗?为什么它不能识别模式</ 和/>?
希望我的问题很清楚。如有必要,很乐意提供更多详细信息/示例。
【问题讨论】:
-
它们是否按照声明的顺序添加到列表中?
-
如果您尝试解析 HTML/XML,我建议改用现有库。
-
您可能想提及
TokenInfo和Pattern的来源。 (假设 oAuth 和 java.util,但是 ... 可能是错误的) -
@RamenChef 我试过更改订单,但这似乎并没有改变任何东西。另外,由于这是一个学校项目,我不能使用外部库。我已将我的 TokenInfo 类添加到 OP。
-
@FieryPhoenix 这与minimal reproducible example 相差甚远。请在您实际匹配您的输入与您的令牌列表的位置添加代码。另外,添加一些示例输入