Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

200
Views
How to use extract substring using regex

I am new to java regex. I need to extract "com.mycomp.war.tasks.JmxMetricsTask" from the below line. 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

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report

0

Well, you could search for a similar question.;

regexp to match java package name

I modified the regex from the top answer to suit your case. I replace the ^…$ (line start/end) portion with \b (word boundaries).

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[a-z][a-z0-9_]*(\.[a-z0-9_]+)+[0-9a-z_]\b/ig

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
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!