简体   繁体   English

Java正则表达式匹配两次匹配空白字符和非空白字符

[英]Java regex match match whitespace and non-whitespace characters 2 times

This is the pattern and string I am using for java regex matching. 这是我用于Java正则表达式匹配的模式和字符串。 I need '/dev/sda6 72342MB 5013MB ' (ie whitespace non whitespce whitespace non whitespace) in a single group. 我需要在单个组中使用'/dev/sda6 72342MB 5013MB ' (即,非空白非空白空格)。

String pattern = ".*\n(\\S+\\s+){2}(.*)";
String str = "Filesystem     1MB-blocks   Used Available Use% Mounted on\n" +
              "/dev/sda6         72342MB 5013MB   63655MB   8% /common";
Pattern r = Pattern.compile(pattern,  Pattern.DOTALL);
Matcher m = r.matcher(str);
System.out.println(m.group(1));

But it is not coming as expected. 但这并没有达到预期。 It is matching 匹配

72342MB 72342兆字节

instead of 代替

/dev/sda6 72342MB / dev / sda6 72342MB

Can anybody tell where am I going wrong ? 有人可以告诉我我要去哪里错吗?

There are two problems in your code. 您的代码中有两个问题。

  • You need to always call, matches() or find() before invoking .group() methods on matcher object. 在匹配器对象上调用.group()方法之前,您需要始终调用, matches()find()
  • Second your regex is incorrectly grouped. 其次,您的正则表达式分组错误。

Currently your group will only give one/last match, so instead you need to wrap whole of your expression into group. 目前,您的小组只会给出一个/最后一场比赛,因此您需要将整个表达式包装成小组。 The correct regex you need is this, 您需要的正确正则表达式是这个,

.*\n((?:\\S+\\s+){2})(.*)

Try this Java codes, 试试这个Java代码,

String pattern = ".*\n((?:\\S+\\s+){2})(.*)";
String str = "Filesystem     1MB-blocks   Used Available Use% Mounted on\n" +
              "/dev/sda6         72342MB 5013MB   63655MB   8% /common";
Pattern r = Pattern.compile(pattern,  Pattern.DOTALL);
Matcher m = r.matcher(str);
if (m.matches()) {
    System.out.println(m.group(1));
}

Prints, 印刷品

/dev/sda6         72342MB 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM