【发布时间】:2014-12-23 03:54:30
【问题描述】:
出于某种原因,我尝试使用 Matcher,但有时它给了我 PatternSyntax Exception 。我知道在我的情况下,这意味着 + 是一个保留字符,应该转义它。但我的字符串中根本没有这样的字符:
Pattern p=Pattern.compile(test,Pattern.CASE_INSENSITIVE);
StringBuffer testing=new StringBuffer (node.getNodeValue());
matcher=p.matcher(testing);
if(!matcher.hitEnd())
{
if(matcher.find())
{
i++;
}
}
在Pattern p=Pattern.compile(test,Pattern.CASE_INSENSITIVE);抛出异常
test 字符串只是一些单词或字符,在任何情况下都不是+
或*等。
这里是 test 将在 for 循环中替换为它们的单词列表:
编辑
我使用了 Elliott Frisch 的答案,但是现在发生了一个奇怪的异常:
for(int j=0;j<index2;j++)
{
test = (test != null) ? test.toLowerCase() : null;
str = (str != null) ? str.toLowerCase() : "";
if (str.contains(test))
{
X[Index]= keArrayList.indexOf(test);
Index++;
}
}
int[] X=new int[100000];
private static final double[] Y=new double[100000];
for(int i=0;i<Index;i++)
{
felan=Y[X[i]];
}
虽然两个循环索引都比100000 小得多,但在第一次迭代中我得到了这个:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
这是否与 if 条件或什么有关?
【问题讨论】:
-
test到底是什么? -
正如我在问题中解释的,这是我从随机网页中提取的一些单词
-
了解实际内容很重要。
-
它是一个很长的列表,它因页面而异,但如果你愿意,我可以向你展示一个给出这个例外的页面
-
@lonesome 我们不需要查看您的列表,我们需要查看您的模式。
标签: java arrays exception pattern-matching matcher