抱歉我的英文。逐个字符地读取文件。我需要输出一个符合我们表达式的字符串,在本例中为([a-zA-Z0-9]){0,}
。我就是这样做的:如果发生换行,那么就检查正则表达式。为什么不起作用?
UDP代码:
public static void main(String[] args) throws FileNotFoundException, IOException {
int endWord = wordToFind.length();
int startWord = 0;
String myWord = "";
int numbLine = 1;
char[] barray = new char[1024];
StringBuilder stringB = new StringBuilder();
Pattern p = Pattern.compile("([a-zA-Z0-9]){0,}");
try(BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("text.txt"))))
{
startTime = System.currentTimeMillis();
int value;
while((value = reader.read(barray, 0, barray.length)) != -1) {
for(int i = 0; i < value; i++) {
stringB.append(barray[i]);
if( Character.toString(barray[i]).equals("\n") ) {
Matcher m = p.matcher(stringB.toString());
if(m.matches()) {
finwWordLine = true;
}
if(finwWordLine) {
System.out.println(numbLine + ": " +stringB.toString());
}
stringB.delete(0, stringB.length());
finwWordLine = false;
numbLine++;
}
}
}
reader.close();
}catch(Exception e){
System.out.println("Error : "+e);
}
}
UPD text.txt
一行
两行
三线 asd asd das dasd asd asdasdasd 五行一个
asdasd asd asd asd
答案 0 :(得分:1)
请尝试使用此正则表达式:
[a-zA-Z0-9]*