Java 扫描文件并收集与模式匹配的完整单词
我正在做一个项目,我需要扫描一个文件夹,然后扫描每个文件中的一个特定单词(比如“@MyPattern”) 我期待着一个设计这样一个场景的最佳方法。 对于初学者,我的工作如下:Java 扫描文件并收集与模式匹配的完整单词,java,string,text,java-8,string-matching,Java,String,Text,Java 8,String Matching,我正在做一个项目,我需要扫描一个文件夹,然后扫描每个文件中的一个特定单词(比如“@MyPattern”) 我期待着一个设计这样一个场景的最佳方法。 对于初学者,我的工作如下: //Read File List<String> lines = new ArrayList<>(); try (Stream<String> stream = Files.lines(Paths.get(fileName))) { stream.
//Read File
List<String> lines = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
stream.forEach(line-> lines.add(line));
} catch (IOException e) {
e.printStackTrace();
}
//Create a pattern to find for
Predicate<String> patternFilter = Pattern
.compile("@MyPattern^(.+)")
.asPredicate();
//Apply predicate filter
List<String> desiredWordsMatchingPattern = lines
.stream()
.filter(patternFilter)
.collect(Collectors.<String>toList());
//Perform desired operation
desiredWordsMatchingPattern.forEach(System.out::println);
//读取文件
列表行=新的ArrayList();
try(Stream=Files.line(path.get(fileName))){
stream.forEach(行->行.添加(行));
}捕获(IOE异常){
e、 printStackTrace();
}
//创建要查找的模式
谓词模式过滤器=模式
.compile(“@MyPattern^(+)”)
.asPredicate();
//应用谓词过滤器
列出所需的单词smatchingpattern=行
.stream()
.过滤器(模式过滤器)
.collect(Collectors.toList());
//执行所需的操作
desiredWordsMatchingPattern.forEach(System.out::println);
即使文件中有多个单词与“@MyPattern”匹配,我也不知道为什么这不起作用。您使用^(+)
的方式在正则表达式中没有意义^
匹配字符串(行)的开头,但字符串的开头不能位于模式之后(仅当模式匹配空字符串时,此处不匹配)。因此,您的模式永远无法匹配任何线条
只需使用:
Predicate<String> patternFilter = Pattern
.compile("@MyPattern")
.asPredicate();
在正则表达式中使用^(+)
的方式没有意义^
匹配字符串(行)的开头,但字符串的开头不能位于模式之后(仅当模式匹配空字符串时,此处不匹配)。因此,您的模式永远无法匹配任何线条
只需使用:
Predicate<String> patternFilter = Pattern
.compile("@MyPattern")
.asPredicate();
以下是我的解决方案:
// can extract annotation and text-inside-parentheses
private static final String REGEX = "@(\\w+)\\((.+)\\)";
//Read File
List<String> lines = Files.readAllLines(Paths.get(filename));
//Create a pattern to find for
Pattern pattern = Pattern.compile(REGEX);
// extractor function uses pattern's second group (text-within-parentheses)
Function<String, String> extractOnlyTextWithinParentheses = s -> {
Matcher m = pattern.matcher(s);
m.find();
return m.group(2);
};
// all lines are filtered and text will be extracted using extractor-fn
Stream<String> streamOfExtracted = lines.stream()
.filter(pattern.asPredicate())
.map(extractOnlyTextWithinParentheses);
//Perform desired operation
streamOfExtracted.forEach(System.out::println);
总结:
您的流式处理非常有效。
您的正则表达式有错误:
- 它几乎匹配一个常量注释,即
@MyPattern
- 您尝试使用括号捕获正确的内容
- 正则表达式中的插入符号
^
- 如果不使用转义括号
和\\(
,您不仅会得到里面的文本,还会得到作为摘录的括号\\)
- 以下是我的解决方案:
// can extract annotation and text-inside-parentheses
private static final String REGEX = "@(\\w+)\\((.+)\\)";
//Read File
List<String> lines = Files.readAllLines(Paths.get(filename));
//Create a pattern to find for
Pattern pattern = Pattern.compile(REGEX);
// extractor function uses pattern's second group (text-within-parentheses)
Function<String, String> extractOnlyTextWithinParentheses = s -> {
Matcher m = pattern.matcher(s);
m.find();
return m.group(2);
};
// all lines are filtered and text will be extracted using extractor-fn
Stream<String> streamOfExtracted = lines.stream()
.filter(pattern.asPredicate())
.map(extractOnlyTextWithinParentheses);
//Perform desired operation
streamOfExtracted.forEach(System.out::println);
总结:
您的流式处理非常有效。
您的正则表达式有错误:
- 它几乎匹配一个常量注释,即
@MyPattern
- 您尝试使用括号捕获正确的内容
- 正则表达式中的插入符号
^
- 如果不使用转义括号
和\\(
,您不仅会得到里面的文本,还会得到作为摘录的括号\\)
@MyPattern
的正则表达式将匹配@MyPattern
,而其他任何东西都不匹配,即它将不匹配@Traces
(为什么应该匹配?)。除此之外,谓词将选择包含匹配项的行,但不会提取匹配项。你可以使用扫描仪。我希望在问你是否接受其中一个(非常不同的)答案时,我不会让你陷入任何忠诚冲突?未来的读者会发现知道哪一个对你更有帮助是很有帮助的。请参阅建议:只需再次检查您的正则表达式一次。看起来您的正则表达式中存在问题。我的字符串类似于:“@Traces(“10869”)@Details(‘用户正在查看用户配置文件’)给定:用户对用户配置文件开放”我期待在@Traces之后提取“10869”。soA正则表达式的正则表达式应该是什么?类似于@MyPattern
的正则表达式将匹配@MyPattern
,而其他任何东西都不匹配,即它将不匹配@Traces
(为什么应该匹配?)。除此之外,谓词将选择包含匹配项的行,但不会提取匹配项。你可以使用扫描仪。我希望在问你是否接受其中一个(非常不同的)答案时,我不会让你陷入任何忠诚冲突?未来的读者会发现知道哪一个对你更有帮助是很有帮助的。看见