作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有如下一系列行(可以按任何顺序出现)
Distal latency 4.9 N/A N/A 4.0 N/A N/A N/A N/A 6.3 4.4 N/A
% failed Chicago Classification 70 1 1 0 1 1 1 1 0 0 1
% panesophageal pressurization 0 0 0 0 0 0 0 0 0 0 0
% premature contraction 20 0 0 1 0 0 0 0 0 1 0
% rapid contraction 10 0 0 1 0 0 0 0 0 0 0
% large breaks 10 0 0 0 0 0 0 0 1 0 0
% small breaks 10 0 0 1 0 0 0 0 0 0 0
我想最终将行标题和每个值提取到哈希中,如下所示
Distallatency=4.9,Distallatency=N/A etc.
failedChicagoClassification1=70,failedChicagoClassification1=1,failedChicagoClassification1=1,failedChicagoClassification1=0,failedChicagoClassification1=1 etc.
and so on
我的策略是:
1. join the words together by replacing the \s between words
2. End the joined word with a character eg : so I can then split each line into an array based on \s
3. Loop through the array adding the line title to each value into a Hash
这是我到目前为止所做的:
Pattern match_patternSwallow2 = Pattern.compile("(?:.*\\d+\\.\\d|N\\/A|\\d*){4,50}");
Matcher matchermatch_patternSwallow2 = match_patternSwallow2.matcher(s);
while (matchermatch_patternSwallow2.find()){
String found = matchermatch_patternSwallow2.group(0).trim();
System.out.println(found);
//Join up the words so can then split by space
found = found.replaceAll("([A-Za-z]+)\\s", "$1_").replaceAll("\\s", ":");
List<String> myList = new ArrayList<String>(Arrays.asList(found.split(":")));
for (int ff=1;ff<myList.size();ff++){
mapSwallow.put(myList.get(0)+"MapSwallowsNum"+ff,myList.get(ff));
}
}
捕获时没有出现任何错误,但它仅在 System.out 行返回一个空字符串。
我做错了什么?
最佳答案
我可以建议以下正则表达式来获取符合您条件的每一行:
"(?m)^\\W*([a-zA-Z].*?)\\s*((?:(?:\\d+(?:\\.\\d+)?|N/A)\\s*)*)$"
请参阅regex demo
详细信息:
(?m)
- 多行模式开启^
- 行的开头\\W*
- 0+ 个非单词字符([a-zA-Z].*?)
-(第 1 组)一个字母,后跟除换行符之外的任意 0 个以上字符,尽可能少,最多\\s*
- 零个或多个空格((?:(?:\\d+(?:\\.\\d+)?|N/A)\\s*)*)
- 第 2 组捕获 0 + 数字序列(后跟一个点和数字(可选))或 N/A
后跟 0+ 个空格$
- 行尾。找到匹配项后,使用 .group(1).replaceAll("\\s+","")
作为键,并拆分 .group(2)
与 .split("\\s+")
来获取值。
查看示例在线代码:
String s = "Distal latency 4.9 N/A N/A 4.0 N/A N/A N/A N/A 6.3 4.4 N/A\n\n % failed Chicago Classification 70 1 1 0 1 1 1 1 0 0 1\n\n % panesophageal pressurization 0 0 0 0 0 0 0 0 0 0 0\n\n % premature contraction 20 0 0 1 0 0 0 0 0 1 0\n\n % rapid contraction 10 0 0 1 0 0 0 0 0 0 0\n\n % large breaks 10 0 0 0 0 0 0 0 1 0 0\n\n % small breaks 10 0 0 1 0 0 0 0 0 0 0";
Pattern match_patternSwallow2= Pattern.compile("(?m)^\\W*([a-zA-Z].*?)\\s*((?:(?:\\d+(?:\\.\\d+)?|N/A)\\s*)*)$");
Matcher matchermatch_patternSwallow2 = match_patternSwallow2.matcher(s);
HashMap<String, String> mapSwallow = new HashMap<String, String>();
while (matchermatch_patternSwallow2.find()){
String[] myList = matchermatch_patternSwallow2.group(2).split("\\s+");
String p1 = matchermatch_patternSwallow2.group(1).replaceAll("\\s+", "");
int line = 1;
for (String p2s: myList){
mapSwallow.put(p1+line, p2s);
line++;
}
}
System.out.println(mapSwallow);
关于java - 如何在java正则表达式中使用嵌套非捕获组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40454768/
我是一名优秀的程序员,十分优秀!