user1950349
user1950349

Reputation: 5146

get everything after a particular string

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?

Upvotes: 1

Views: 665

Answers (3)

bobble bubble
bobble bubble

Reputation: 18490

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.

Upvotes: 1

riteshtch
riteshtch

Reputation: 8769

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

Upvotes: 1

retrography
retrography

Reputation: 6812

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.

Upvotes: 1

Related Questions