Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用java构建解析文本文件的数据结构_Java_Android_String_Parsing_Design Patterns - Fatal编程技术网

用java构建解析文本文件的数据结构

用java构建解析文本文件的数据结构,java,android,string,parsing,design-patterns,Java,Android,String,Parsing,Design Patterns,在文本文件中,我有以下文本 add device 1: /dev/input/event7 name: "evfwd" add device 2: /dev/input/event6 name: "aev_abs" add device 3: /dev/input/event5 name: "light-prox" add device 4: /dev/input/event4 name: "qtouch-touchscreen" add devi

在文本文件中,我有以下文本

add device 1: /dev/input/event7
  name:     "evfwd"
add device 2: /dev/input/event6
  name:     "aev_abs"
add device 3: /dev/input/event5
  name:     "light-prox"
add device 4: /dev/input/event4
  name:     "qtouch-touchscreen"
add device 5: /dev/input/event2
  name:     "cpcap-key"
add device 6: /dev/input/event1
  name:     "accelerometer"
add device 7: /dev/input/event0
  name:     "compass"
add device 8: /dev/input/event3
  name:     "omap-keypad"
我需要做的是,对于每个name元素,我需要提取相应的事件号,并将它们放在hashmap上

例如,从文本中我们看到nameevfwdevent7链接,因此hashmap如下所示

<"evfwd", 7>

同样地

<"compass", 0>


如何有效地解析文本并构建类似java的哈希映射?

您可能应该使用正则表达式(regex)来解析信息。要使用正则表达式,请查看Pattern和Matcher类,下面是一个示例:

Pattern pattern = Pattern.compile("test");
Matcher matcher = pattern.matcher("some testing thing with a test in it");
然后可以调用matcher.find()将matchers内容设置为下一个匹配项(如果找到另一个匹配项,则返回true,如果到达末尾,则返回false)

您需要一个包含所谓捕获组的正则表达式,基本上每当您在正则表达式中放置()时,您都可以调用
matcher.group(index)
来查找这些()的内容(应该注意,.group(0)返回整件事,.group(1)第一个()的内容,等等)

这是我刚刚制作的一个正则表达式,应该适合您:

adddevice\d*?:/dev/input/event(\d*?)\n name:\“(.*?\”

这句话用英语说:

添加设备[一些号码]:/dev/input/event[一些号码,捕获组#1]\n name:\“[一些字符串,捕获组#2]\”

要获取它的事件号,请调用
Integer.parseInt(matcher.group(1))
,要获取它的名称,请调用
matcher.group(2)


如果有什么失败,请告诉我,希望我能帮上忙:)

您可能应该使用正则表达式(regex)来解析信息。要使用正则表达式,请查看Pattern和Matcher类,下面是一个示例:

Pattern pattern = Pattern.compile("test");
Matcher matcher = pattern.matcher("some testing thing with a test in it");
然后可以调用matcher.find()将matchers内容设置为下一个匹配项(如果找到另一个匹配项,则返回true,如果到达末尾,则返回false)

您需要一个包含所谓捕获组的正则表达式,基本上每当您在正则表达式中放置()时,您都可以调用
matcher.group(index)
来查找这些()的内容(应该注意,.group(0)返回整件事,.group(1)第一个()的内容,等等)

这是我刚刚制作的一个正则表达式,应该适合您:

adddevice\d*?:/dev/input/event(\d*?)\n name:\“(.*?\”

这句话用英语说:

添加设备[一些号码]:/dev/input/event[一些号码,捕获组#1]\n name:\“[一些字符串,捕获组#2]\”

要获取它的事件号,请调用
Integer.parseInt(matcher.group(1))
,要获取它的名称,请调用
matcher.group(2)


如果有什么失败,请告诉我,希望我能帮上忙:)

我会坚持基本原则

编写一个函数,首先读取4个单词,然后取第4个单词。使用“/”作为分隔符,将第4个单词解析为3个不同的单词。取最后一个,读取最后一个字符,并将其作为hashmap的值

然后继续读另外两个单词,最后一个单词作为键和前面读到的值存储到hashmap中


递归地应用此函数,直到到达文件末尾。

我将介绍基本内容

编写一个函数,首先读取4个单词,然后取第4个单词。使用“/”作为分隔符,将第4个单词解析为3个不同的单词。取最后一个,读取最后一个字符,并将其作为hashmap的值

然后继续读另外两个单词,最后一个单词作为键和前面读到的值存储到hashmap中


递归应用此函数,直到到达文件末尾。

这些字符串是否始终格式正确,即始终具有相同的格式?是的,它们是硬件生成的。这些字符串是否始终格式正确,即始终具有相同的格式?是的,它们是硬件生成的。为什么使用递归?最后一个单词的最后一个字符可能不起作用,因为他可能有两个数字的设备(谁知道呢?)。为什么要使用递归?最后一个单词的最后一个字符可能不起作用,因为他可能有两位数的设备(谁知道呢?)。正则表达式会让你的生活更轻松。谢谢。我正在使用正则表达式,但在找出正确的正则表达式时遇到了问题。您的解决方案似乎正常工作。您好@Alex我尝试了您的正则表达式,代码如下:String regexP=“add device\\d*?:/dev/input/event(\\d*?)\n name:\”(.*?\”;Pattern p=Pattern.compile(regexP);匹配器匹配器=p.Matcher(线);但是它不起作用。正则表达式会让你的生活更轻松。谢谢。我正在使用正则表达式,但在找出正确的正则表达式时遇到了问题。您的解决方案似乎正常工作。您好@Alex我尝试了您的正则表达式,代码如下:String regexP=“add device\\d*?:/dev/input/event(\\d*?)\n name:\”(.*?\”;Pattern p=Pattern.compile(regexP);匹配器匹配器=p.Matcher(线);但它不起作用。