I am trying unsuccessfully to use Java to match a line so I can use particular parts of the line. The String line is:
String line = "-rw-rw-r-- 1 sherman sherman 0 Feb 14 12:17 nonpedalSwing";
I am using Pattern and Matcher. This is an example of how I want to seperate the line so I can use sub 2, sub 3, and sub 5.
String Pattern = "^(-rw-rw-r-- 1 sherman sherman 0)(Feb)(14)( 12:17 )(nonpedalSwing)";
This does not work for me:
String Pattern = "^(-\\w+-\\w+-\\w--\\s\\d\\s\\w+\\s\\w+\\s+\\d\\s)(\\w+)(\\s)(\\d+)(\ \s\\d+:\\d+\\s)(\\w+)";
At the end of this message is the code I am playing with.
How would you do it?
Thanks; Sherman
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class equalEquals
{
/**
* Constructor for objects of class equalEquals
*/
public static void main(String arg[])
{
String line = "-rw-rw-r-- 1 sherman sherman 0 Feb 14 12:17 nonpedalSwing";
// Create a Pattern object
String pattern = "^(\\w+)(\\s)(\\d+)(\\s)(\\w+)";
Pattern r = Pattern.compile(pattern);
// Create a Matcher object
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println("Found value: \"" + m.group(0) + "\"");
System.out.println("Found value: \"" + m.group(1) + "\"");
System.out.println("Found value: \"" + m.group(2) + "\"");
System.out.println("Found value: \"" + m.group(3) + "\"");
// System.out.println("Found value: \"" + m.group(4) + "\"");
} else {
System.out.println("No match");
}
}
}