使用Java解析器在txt文件中搜索特定单词 [英] searching for specific words in the txt file using java parser

查看:155
本文介绍了使用Java解析器在txt文件中搜索特定单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下txt fil,我想知道如何解析它并搜索Cell和Engin单词,我想要的是打印出包含Cell和Engine单词的方法名称.以下是txt文件,请不要将其视为Java代码,因为我已经将其移至txt文件以进行解析.

@Test
公共无效testGetMonsters(){
单元格cell11 = aBoard.getCell(1,1);
theEngine =新引擎(theGame);
}

@Test
公共无效testDxDyPossibleMove(){
单元格cell11 = aBoard.getCell(1,1);
}
解析的输出如下:testGetMonsters方法包含单元格和引擎单词
testDxDyPossibleMove方法包含单元格单词

I have the following txt fil, I''m wondering how can I pars it and search for Cell And Engin words, what I want is to print out the method name that contains Cell, Engine words. the following is the txt file don''t look at it as java code since I already moved it to txt file for parsing purposes.

@Test
public void testGetMonsters() {
Cell cell11 = aBoard.getCell(1, 1);
theEngine = new Engine(theGame);
}

@Test
public void testDxDyPossibleMove() {
Cell cell11 = aBoard.getCell(1, 1);
}
the output of parsing looks like: testGetMonsters method contains Cell and Engine words
testDxDyPossibleMove method contains Cell word

推荐答案

如果单词Cell Engine 必须为 ^ ],正如您的用法示例所建议的那样,没有办法使用可以解析Java语言的真实解析器.如果单词可以出现在任何上下文中,则可以使用正则表达式 [ ^ ]确保您要搜索的单词由以下字符分隔单词边界字符("\ b").

该站点可以帮助您构建Java解析器,因为已经有完整的Java语法示例: http://www.antlr.org/ [^ ].

如果您有任何疑问或疑问,请随时发表评论.

干杯!

—MRB
If the words Cell and Engine have to be Java types[^], as your usage sample suggests, there is no way around using a real parser that can parse the Java language. If the words can appear in any context you could go after them with regular expressions[^] making sure that the words you are searching for are delimited by word boundary characters ("\b").

This site can help you build a Java parser as there are already samples for the complete Java grammar: http://www.antlr.org/[^].

If you have any doubts or questions, feel free to leave a comment.

Cheers!

—MRB


这篇关于使用Java解析器在txt文件中搜索特定单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆