0

I have created a program which gets the string from the putty and kills the processID from getting processID from that string. Currently it is working fine but sometimes it is not reliable.

Please find the target and the regex I used below.

Target String: root 14139 1 25 Aug03 ? 06:47:50 /usr/local

RegEx I used: \\\d{3,5}

I want to get the RegEx which can find the immediate number after root and ignore the other text. (e.g., I want to extract 14139 from the example string removing all the extra space.)

How can I do it?

6
  • You used a regex and what was the result? Also, if you want to match "root", you can always add it to the regex pattern, have you tried? Commented Aug 4, 2016 at 12:48
  • s = s.replaceFirst("^.*\\broot\\s+(\\d{3,5}).*$", "$1"); Commented Aug 4, 2016 at 12:51
  • try this regex (?:root )[0-9]+ Commented Aug 4, 2016 at 12:51
  • Hello @WiktorStribiżew - Your code didn't work as expected. I want to extract the string which comes after root, that is 14139. Commented Aug 4, 2016 at 13:32
  • @Rishaldevsingh - Your code gives me null value. Commented Aug 4, 2016 at 13:32

5 Answers 5

3

So you need the a number from the second field delimited by spaces. The following regex gets it in the capture group 1:

^\S+\s+(\d+)

Demo: https://regex101.com/r/bE7xL8/1

And the sample Java code:

String input = "root 14139 1 25 Aug03 ? 06:47:50 /usr/local";
Pattern pattern = Pattern.compile("^\\S+\\s+(\\d+)");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
    System.out.println(matcher.group(1));
}

Demo: https://ideone.com/LVLCNm

Sign up to request clarification or add additional context in comments.

3 Comments

Your code works in the demo, but how to utilize them in java code, it gives me the error message.
It gives me output with root, I want to exclude root and just want the digits.
The root is a part of the whole regex match while you need only the capture group 1 as it is stated in the answer. Please use Matcher.group() method to retrieve this capture. The example in ideone.com/LVLCNm shows how to use it.
0

This will be a bit more forgiving and less dependent on number of spaces, again using a capturing group:

root[^\d]*(\d+)

Comments

0

I suggest this one:

^root (\d*)\D?.*

capture 1 or more digits after a starting "root " string and delimited by not-digit chars

tester: https://regex101.com/r/sL8oJ7/1

Comments

0

You can use following regex and used first group : root[^\S]*(\d+)

Demo and Example

Comments

0

Try this for your target string its giving output as required by you.

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class exampleproblem {
public static void main(String[] args) {
    String target = "root 14139 1 25 Aug03 ?        06:47:50 /usr/local";
    String regexex = "root(.[0-9]+)";
    Pattern pattern = Pattern.compile(regexex);
    Matcher matcher = pattern.matcher(target);
    while (matcher.find()) {
        System.out.println(matcher.group(1).trim());
    }
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.