简体   繁体   English

如何使用正则表达式使用提取子字符串

[英]How to use extract substring using regex

I am new to java regex. 我是Java regex的新手。 I need to extract "com.mycomp.war.tasks.JmxMetricsTask" from the below line. 我需要从下面的行中提取“ com.mycomp.war.tasks.JmxMetricsTask”。 How can i do with regex? 我该如何使用正则表达式?

String test = "id=         com.mycomp.war.tasks.JmxMetricsTask      I run/id-geLh3hM1-1_2 [Svc--DAG]";

Is it too complicated? 太复杂了吗? Is it possible by regex? 正则表达式可能吗? I need to extract above line in return? 我需要提取上面的行作为回报吗?

VG VG

You need to define your problem and requirements more thoroughly. 您需要更彻底地定义您的问题和要求。

For the example you show, there is a much simpler solution: 对于您显示的示例,有一个简单得多的解决方案:

String test = "id=         com.mycomp.war.tasks.JmxMetricsTask      I run/id-geLh3hM1-1_2 [Svc--DAG]";
String answer = test.substring(3).trim().split(" ", 2)[0];

Disclaimer: this might not work as intended for all of your possible inputs. 免责声明:这可能不适用于您所有可能的输入。 This is why I say that you need to completely define your situation. 这就是为什么我说您需要完全定义您的情况。 If all of your inputs match the assumptions I made based on your one example, then this would work without using regular expressions. 如果所有输入都符合我根据您的一个示例所做的假设,则无需使用正则表达式就可以工作。

There is no need for Regex. 不需要正则表达式。 You can do it like 你可以像

String test = "id=         com.mycomp.war.tasks.JmxMetricsTask      I run/id-geLh3hM1-1_2 [Svc--DAG]";
String subTest = "com.mycomp.war.tasks.JmxMetricsTask";
test.substring(test.indexOf(subTest), subTest.length() + test.indexOf(subTest));

But can you explain your actual requirements?? 但是您能解释一下您的实际需求吗? using above, you can get the required string part 在上面使用,您可以获得所需的字符串部分

Well, you could search for a similar question.; 好吧,您可以搜索类似的问题。

regexp to match java package name regexp以匹配Java包名称

I modified the regex from the top answer to suit your case. 我从最上面的答案修改了正则表达式以适合您的情况。 I replace the ^…$ (line start/end) portion with \\b (word boundaries). 我用\\b (单词边界)替换^…$ (行的开始/结束)部分。

import java.util.regex.*;

public class RegexTest {
    public static final String PACKAGE_PATTERN = "\\b[a-z][a-z0-9_]*(\\.[a-z0-9_]+)+[0-9a-z_]\\b";

    public static void main(String[] args) {
        String s = "id=         com.mycomp.war.tasks.JmxMetricsTask      I run/id-geLh3hM1-1_2 [Svc--DAG]";
        Pattern p = Pattern.compile(PACKAGE_PATTERN, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(s);

        if (m.find()) {
            System.out.println(m.group()); // com.mycomp.war.tasks.JmxMetricsTask
        }
    }
}

Here's a live example using Regex 101: /\\b[az][a-z0-9_]*(\\.[a-z0-9_]+)+[0-9a-z_]\\b/ig 这是一个使用Regex 101的实时示例:/ /\\b[az][a-z0-9_]*(\\.[a-z0-9_]+)+[0-9a-z_]\\b/ig

https://regex101.com/r/RwJtLK/2 https://regex101.com/r/RwJtLK/2


You could also just split by whitespace characters and grab the second token. 您也可以按空格字符分开并获取第二个标记。

public class RegexTest {        
    public static void main(String[] args) {
        String s = "id=         com.mycomp.war.tasks.JmxMetricsTask      I run/id-geLh3hM1-1_2 [Svc--DAG]";
        String[] tokens = s.split("\\s+");

        System.out.println(tokens[1]); // com.mycomp.war.tasks
    }
}

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

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