简体   繁体   English

获取特定字符串后的所有内容

[英]get everything after a particular string

I have a String coming as "process_client_123_Tree" and "process_abc_pqr_client_123_Tree" . 我有一个字符串来自"process_client_123_Tree""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. 我想在"process_client_""process_abc_pqr_client_"之后提取所有内容并将其存储在String变量中。

Here currentKey variable can contain either of above two strings. 这里currentKey变量可以包含以上两个字符串中的任何一个。

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? 我应该在这里使用拆分还是一些正则表达式?

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) process_(client_ | abc_pqr_client_)匹配“process_”后跟“client_”或abc_pqr_client_“(作为组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 表示任何char和*表示0次或更多次,因此它匹配字符串中的其余字符直到结束($)并将其捕获为组2

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. 因此,我使用该正则表达式创建一个模式对象,然后使用匹配器对象来获取已匹配的字符串部分。

A regex pattern could be: "process_(?:abc_pqr_)?client_(\\\\w+)" regex101 demo 正则表达式模式可能是: "process_(?:abc_pqr_)?client_(\\\\w+)" regex101演示

Demo at RegexPlanet . 在RegexPlanet演示 Matches will be in group(1) / first capturing group . 匹配将在group(1) /第一个捕获组中


To extend it with limit to the right, match lazily up to the right token 要将其限制为向右扩展,请懒惰地匹配到正确的令牌

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

where \\w+? 哪里\\w+? matches as few as possible word characters to meet condition. 匹配尽可能少的单词字符以满足条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM