I need to save parts of a String in different variables using a single regular expression, the string is the following and it can vary, but the structure will always be the same (this line is stored in the variable strLineProcess):
Skype.exe pid: 1404 WATCHOUT\tofetopo
I have the following code:
final String regex = "^(\S*)";
final String processDetails = strLineProcess;
String processName = null;
String processID = null;
String computerName = null;
String currentUser = null;
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(processDetails);
if (matcher.find()) {
processName = matcher.group(1);
processID = matcher.group(2);
computerName = matcher.group(3);
currentUser = matcher.group(4);
}
The regular expression in the regex variable correctly matches the name of the process, but I do not know how to concatenate regular expressions to save the following parts of the String. For example, the regular expression ((\S*)+([0-9]))
matches the processID (which in this example would be 1404), but I need to concatenate it with the first expression to have everything in one, just as I should do with computerName and currentUser, and here is where I'm lost . Any ideas?
The expected output in this example is:
processName = Skype.exe
processID = 1404
computerName = WATCHOUT
currentUser = tofetopo
The name of the process can be any up to the first space, the processID only numbers, computerName always appears just after the processID and even before the "\"
, and the currentUser everything that comes after the "\"
.