Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.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 - Fatal编程技术网

如何在java正则表达式中使用嵌套的非捕获组

如何在java正则表达式中使用嵌套的非捕获组,java,regex,Java,Regex,我有一系列行,如下所示(可以是任何顺序) 我希望最终将行标题和每个值提取到一个哈希中,如下所示 Distallatency=4.9,Distallatency=N/A etc. failedChicagoClassification1=70,failedChicagoClassification1=1,failedChicagoClassification1=1,failedChicagoClassification1=0,failedChicagoClassification1=1 etc.

我有一系列行,如下所示(可以是任何顺序)

我希望最终将行标题和每个值提取到一个哈希中,如下所示

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\u patterns=Pattern.compile((?:.\\d+\\.\\d\N\\/A\\\\d*){4,50});
匹配器匹配器匹配器匹配器匹配器匹配器模式2=匹配器匹配器模式2.匹配器;
while(matchermatch_.2.find()){
找到的字符串=matchermatch_.2.group(0.trim();
System.out.println(已找到);
//将单词连接起来,然后按空格分开
found=found.replaceAll(([A-Za-z]+)\\s“,“$1”).replaceAll(\\s“,”:”);
List myList=newarraylist(Arrays.asList(found.split(“:”));

对于(int ff=1;ff,我可以建议使用以下正则表达式来获得符合您标准的每一行:

"(?m)^\\W*([a-zA-Z].*?)\\s*((?:(?:\\d+(?:\\.\\d+)?|N/A)\\s*)‌​*)$"

详细信息

  • (?m)
    -多行模式打开
  • ^
    -行的开头
  • \\W*
    -0+非单词字符
  • ([a-zA-Z].*)
    -(第1组)一个字母,后跟除换行符以外的任何0+字符,尽可能少至
  • \\s*
    -零个或多个空格
  • ((?:(?:\\d+(?:\\.\\d+)\124;不适用)\\s*)‌​*)-第2组捕获0+个数字序列(后跟一个点和数字,可选)或
    N/a
    后跟0+空格
  • $
    -行结束
找到匹配项后,使用
.group(1).replaceAll(“\\s+”,”)
作为键,并使用
.split(“\\s+”)
拆分
.group(2)
以获取值

请参阅联机代码示例:

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);
字符串s="远端潜伏期4.9 N/A N/A 4.0 N/A N/A N/A N/A N/A N/A N/A 6.3 4.4 N/A\N%芝加哥分类不合格70 1 0 1 1 1 1 1 1 1 1 1 0 0 1\N\N%全食管加压0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\N\N%早搏0 0 0 0 0 0 0 0 0 0 0 0 0 0\N\N%快速收缩10 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\N\N%1 0 0 0 0 0 0 0";
模式匹配\u patterns=Pattern.compile((?m^\\W*([a-zA-Z].*)\\s*((?:(?::\\d+(?:\\.\\d+)?\N/a)\\s*)$;
匹配器匹配器匹配器匹配器匹配器匹配器模式2=匹配器匹配器模式2.匹配器;
HashMap mapSwallow=新的HashMap();
while(matchermatch_.2.find()){
字符串[]myList=matchermatch_2.group(2.split(\\s+);
字符串p1=matchermatch_.2.group(1.replaceAll(“\\s+”,”);
内线=1;
for(字符串p2s:myList){
MAPSWOWN.put(p1+线,p2s);
line++;
}
}
系统输出打印LN(mapSwallow);

您是逐行处理吗?我将整个文档作为字符串,然后在上面进行模式匹配。不是“while”只处理所有匹配,因此我不需要逐行处理吗?不知道,如果没有可复制的示例,很难提供帮助。请尝试regex。然后使用
组(1)。replaceAll(\\s+,“”)
作为键,并使用
拆分
.group(2)
.split(\\s+”)
以获取值。类似OK的内容似乎适用于大多数数据。请作为答案发布,我将投赞成票
"(?m)^\\W*([a-zA-Z].*?)\\s*((?:(?:\\d+(?:\\.\\d+)?|N/A)\\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);