解析txt文件并搜索特定单词
我有以下文本文件,我想知道如何解析它并搜索 Cell 和 Engine 单词,我想要的是打印出包含 Cell、Engine 单词的方法名称。以下是 txt 文件,不要将其视为 java 代码,因为我已经将其移动到 txt 文件以进行解析。
@Test
public void testGetMonsters() {
Cell cell11 = aBoard.getCell(1, 1);
theEngine = new Engine(theGame);
}
@Test
public void testDxDyPossibleMove() {
Cell cell11 = aBoard.getCell(1, 1);
}
所需的解析输出如下所示:
testGetMonsters class contains Cell and Engine words
testDxDyPossibleMove class contains Cell word
I have the following text fil, I'm wondering how can I parse it and search for Cell And Engine 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 desired output of parsing looks like:
testGetMonsters class contains Cell and Engine words
testDxDyPossibleMove class contains Cell word
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想要使用正则表达式和 Java 的模式匹配工具。请查看正则表达式和 Java 编程语言以获取示例用法。
简单的例子:
确定您是否/属于哪个班级是另一个问题......您需要一个“词法分析器”。 JavaCC 是一个很好的起点。
You probably want to use regular expressions and the pattern matching facility of Java. Take a look at Regular Expressions and the Java Programming Language for example usage.
Quick example:
Determining if / what class you're in is another question... you'll need a "lexer" for that. JavaCC is a good starting point there.
我没有看到您的代码和问题之间的链接,但是:
I don't see the link between your code and your question but :
哒哒哒!
好吧,它不是 100% 可靠,但它是一个开始。
编辑:或者你可以使用像 KayKay 建议的扫描仪,但这有什么乐趣:D
Tadaaaa!
Alright, it ain't 100% reliable, but it's a start.
EDIT: or you can use a Scanner like KayKay suggested, but where's the fun in that :D
虽然我不确定我是否理解你的问题,但这里有一些提示。
要获取类名,请使用 obj.getClass().getName()。
要搜索字符串,请使用 str.contains("Cell") 或使用正则表达式。看一下
java.util.Pattern
类。Although I am not sure I understand your question here are some tips.
To get class name use
obj.getClass().getName()
.To search into string use str.contains("Cell") or use regular expressions. Take a look on class
java.util.Pattern
.