我在Java中,以下正则表达式 -正则表达式不考虑空间
Pattern p = Pattern.compile("int|float|char\\s\\w");
但仍这是符合“intern
”了。
整个代码 -
package regex;
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class Regex {
public static void main(String[] args) throws IOException{
// TODO code application logic here
int c = 0;
BufferedReader bf = new BufferedReader(new FileReader("new.c"));
String line;
Pattern p = Pattern.compile("int|float|char\\s\\w");
Matcher m;
while((line = bf.readLine()) != null) {
m = p.matcher(line);
if(m.find()) {
c++;
}
}
System.out.println(c);
}
}
不是重复的,引用的问题是关于贪婪,这个是关于运算符的优先级。 – SJuan76
尝试发布文件内容,然后你想阅读,以帮助解答 – Abe
[正则表达式只匹配整个单词](https://stackoverflow.com/questions/1751301/regex-match-entire-words-only )。所有你需要的是''int \\ b | float | char \\ s \\ w“'以避免在'intern'中匹配'int'。 –