Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/331.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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_Regex_Collections_Hashmap - Fatal编程技术网

“保存”;(键、值)“;使用java正则表达式从文件到映射的字符串

“保存”;(键、值)“;使用java正则表达式从文件到映射的字符串,java,regex,collections,hashmap,Java,Regex,Collections,Hashmap,我能够从文件中读取内容 (abcd, 01) (xyz,AB) (pqrst, 1E) 我想将此内容保存到mapas Map<String, String> map=new HashMap<>(); map.put("abcd","01"); map.put("xyz","AB"); map.put("pqrst","1E"); Map Map=newhashmap(); 地图放置(“abcd”、“01”); 地图放置(“xyz”、“AB”); 地图放置(“pqrst

我能够从文件中读取内容

(abcd, 01)
(xyz,AB)
(pqrst, 1E)
我想将此内容保存到
map
as

Map<String, String> map=new HashMap<>();
map.put("abcd","01");
map.put("xyz","AB");
map.put("pqrst","1E");
Map Map=newhashmap();
地图放置(“abcd”、“01”);
地图放置(“xyz”、“AB”);
地图放置(“pqrst”、“1E”);

请帮助我使用java中的正则表达式以映射形式获取内容

我假设您可以使用
BufferedReader.readLine()
或类似工具阅读每一行。调用该字符串
。然后放下括号:

line = line.substring(1,line.length()-1);
那么,您只需要进行拆分:

String[] bits = line.split(",");
map.put(bits[0], bits[1]);

问题是关于使用regexp,但假设这不是一个类赋值等,则解决方案不需要regexp。每个键还处理多个值的简短解决方案:

Map<String, List<String>> map = Files.lines(Paths.get("data.txt")).map(s -> s.replace("(", "").replace(")", ""))
                .collect(groupingBy(s -> (s.split(","))[0], mapping(s -> (s.split(",", -1))[1].trim(), toList())));

System.out.println(map);
说明:

Files.lines(...) //reads all lines from file as a java 8 stream
map(s-> s.replace) // removes all parenthesis from each line
collect(groupingBy(s -> (s.split(","))[0] //collect all elements from the stream and group them by they first part before the ","
mapping(mapping(s -> (s.split(",", -1))[1].trim(), toList()) //and the part after the "," should be trimmed, -1 makes it able to handle empty strings, collect those into a list as the value of the previously grouping
替代替换,而不是两个简单的替换,以删除(和)您可以使用

s.replaceAll("\\(|\\)", "")
不确定哪一个可读性最好。

Pattern=Pattern.compile(“^\\([a-z]+),([0-9A-z]+)\\)$”;
Pattern pattern = Pattern.compile("^\\(([a-z]+), ([0-9A-Z]+)\\)$");
Map<String, String> map = Files.lines(path)
    .map(pattern::matcher)
    .collect(toMap(x -> x.group(1), x -> x.group(2)));
Map Map=文件.行(路径) .map(模式::匹配器) .collect(toMap(x->x.group(1),x->x.group(2));
匹配组1是
[a-z]+
-键。 匹配组2为
[0-9A-Z]+
-值。 根据你的需要改变团队模式


注意:正则表达式是一种强大的机制。如果或者,正确地说,当输入数据变得复杂时,您的模式将增长并变得不可理解。

发布您尝试过的内容?请参阅,并在此处提供帮助。您使用哪一版本的Java?流正在从文件读取并保存到映射中,对吗?最好执行
s.replaceAll(“^\\(|\\)$”,“”)
只删除外括号。谢谢,我得到了它
Pattern pattern = Pattern.compile("^\\(([a-z]+), ([0-9A-Z]+)\\)$");
Map<String, String> map = Files.lines(path)
    .map(pattern::matcher)
    .collect(toMap(x -> x.group(1), x -> x.group(2)));