简体   繁体   中英

Java regex match match whitespace and non-whitespace characters 2 times

This is the pattern and string I am using for java regex matching. I need '/dev/sda6 72342MB 5013MB ' (ie whitespace non whitespce whitespace non whitespace) in a single group.

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

instead of

/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.
  • 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,

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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