我在测试人员中确定我的正则表达式应该适用于我想要查找的行,但是当我使用文本文件中的输入时,即使我searchString
我也不会得到匹配是.*the.*
任何建议都将受到赞赏。
FileReader fr = new FileReader("ValenciaCourses.txt");
BufferedReader br = new BufferedReader(fr);
String myRegex = null;
int count = 0;
br.mark(2000);
while(br.readLine() != null) {
br.reset();
try {
myRegex = br.readLine();
Pattern pattern = Pattern.compile(myRegex);
String searchString = ".*[3-6](\\s*)";
Matcher matcher = pattern.matcher(searchString);
boolean found = false;
if (matcher.find()) {
count++;
}
}catch (PatternSyntaxException pse) {
System.out.println(myRegex);
}finally {
}
br.mark(2000);
答案 0 :(得分:0)
看起来你正在编译从文件中读取的行而不编译正则表达式
你必须编译你的正则表达式而不是文件中的行
这可能会对你有所帮助
FileReader fr = new FileReader("ValenciaCourses.txt");
BufferedReader br = new BufferedReader(fr);
String myRegex = ".*[3-6](\\s*)";
Pattern pattern = Pattern.compile(myRegex);
String currentLine=null;
int count = 0;
br.mark(2000);
while((currentLine=br.readLine()) != null) {
try {
Matcher matcher = pattern.matcher(currentLine);
boolean found = false;
if (matcher.find()) {
count++;
}
}catch (PatternSyntaxException pse) {
System.out.println(myRegex);
}