1

I have a String coming as "process_client_123_Tree" and "process_abc_pqr_client_123_Tree". I want to extract everything after "process_client_" and "process_abc_pqr_client_" and store it in a String variable.

Here currentKey variable can contain either of above two strings.

String clientId = // how to use currentKey here so that I can get remaining portion in this variable

What is the right way to do this? Should I just use split here or some regex?

user1950349
  • 3,822
  • 15
  • 47
  • 87
  • 1
    either:- use a Pattern or test with startswith and then substring – seneque Feb 24 '16 at 00:43
  • What is the Pattern will look like? I have a if check already with `startswith` for both of those two above strings and I need to do this inside that if block only so it looks like I can use Pattern only. – user1950349 Feb 24 '16 at 00:45
  • 1
    Try `String clientId = currentKey.replaceFirst("process.*client_", "");`. – saka1029 Feb 24 '16 at 00:55
  • @saka1029 This works as long as the desired part of the string does not contain another `client_`! `.*` is greedy and will eat into the rest of the string. – retrography Feb 24 '16 at 01:16

3 Answers3

1
import java.util.regex.*;

class test
{
    public static void main(String args[])
    {
        Pattern pattern=Pattern.compile("^process_(client_|abc_pqr_client_)(.*)$");
        Matcher matcher = pattern.matcher("process_client_123_Tree");
        while(matcher.find())
            System.out.println("String 1 Group 2: "+matcher.group(2));
        matcher = pattern.matcher("process_abc_pqr_client_123_Tree");
        while(matcher.find())
            System.out.println("String 2 Group 2: "+matcher.group(2));

        System.out.println("Another way..");

        System.out.println("String 1 Group 2: "+"process_client_123_Tree".replace("process_client_", ""));
        System.out.println("String 2 Group 2: "+"process_abc_pqr_client_123_Tree".replace("process_abc_pqr_client_", ""));
    }
}

Output:

$ java test
String 1 Group 2: 123_Tree
String 2 Group 2: 123_Tree
Another way..
String 1 Group 2: 123_Tree
String 2 Group 2: 123_Tree

Regex breakup:

^ match start of line
process_(client_|abc_pqr_client_) match "process_" followed by "client_" or abc_pqr_client_" (captured as group 1)
(.*)$ . means any char and * means 0 or more times, so it match the rest chars in string until end ($) and captures it as group 2

riteshtch
  • 8,189
  • 4
  • 19
  • 35
  • Thanks it works fine. Also what will be the regex for this if I want to extract everything between these two string `process_client_` and `_trace_count` inside this string `process_client_123_Tree_trace_count` so I want to extract `123_Tree` from that String. – user1950349 Feb 24 '16 at 01:12
  • `^process_client_(.*)_trace_count$` -- And again you only have one matching group. – retrography Feb 24 '16 at 01:13
  • `^process_client_(.*)_trace_count$` would be the regex – riteshtch Feb 24 '16 at 01:13
1
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Matchit{

   public static void main(String []args){
      String str = "process_abc_pqr_client_123_Tree";
      Pattern p = Pattern.compile("process_abc_pqr_client_(.*)|process_client_(.*)");
      Matcher m = p.matcher("process_abc_pqr_client_123_Tree");
      if (m.find( )) {
            System.out.println("Found value: " + m.group(1) );
      }
   }
}

Gets you:

123_Tree

The parentheses in the regexp define the match groups. The pipe is a logical or. Dot means any character and star means any number. So, I create a pattern object with that regexp and then use a matcher object to get the part of the string that has been matched.

retrography
  • 4,972
  • 3
  • 16
  • 27
1

A regex pattern could be: "process_(?:abc_pqr_)?client_(\\w+)"  regex101 demo

Demo at RegexPlanet. Matches will be in group(1) / first capturing group.


To extend it with limit to the right, match lazily up to the right token

"process_(?:abc_pqr_)?client_(\\w+?)_trace_count"

where \w+? matches as few as possible word characters to meet condition.

Community
  • 1
  • 1
bobble bubble
  • 11,968
  • 2
  • 22
  • 34