0

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

Michael
  • 41,989
  • 11
  • 82
  • 128
vvg
  • 51
  • 7
  • 1
    See [Learning Regular Expressions](http://stackoverflow.com/questions/4736/learning-regular-expressions). Check [regexone.com](https://regexone.com/). It is quite easy even for a novice. – Wiktor Stribiżew Apr 28 '17 at 13:02
  • You don't need regex for this. – Michael Apr 28 '17 at 13:04
  • Possible duplicate of [regexp to match java package name](http://stackoverflow.com/questions/29783092/regexp-to-match-java-package-name) – Michael Apr 28 '17 at 13:10
  • Hi.. The requirement is, i need to search the string so as to include or skip the lines which do not follow the pattern.And if they follow the pattern,I need to extract the line "com.mycomp.war.tasks.JmxMetricsTask" this string. This is just class name. It could be something else.. like "a.b.c". I need to know the class name from here. Similarly i need to extract next string after delimiter information [Svc--DAG]. VG – vvg May 02 '17 at 06:25

3 Answers3

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.

dsh
  • 12,037
  • 3
  • 33
  • 51
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

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92
Afridi
  • 6,753
  • 2
  • 18
  • 27
  • Why would you search for the thing you are trying to find? Isn't that the whole point of searching? I have no clue why you would do that? – Mr. Polywhirl Apr 28 '17 at 13:18
  • right, and that's why I asked what's the actual requirement? Because if you have already the required string, then there is no need to search for it – Afridi Apr 28 '17 at 13:21
  • That's a good question ! But not a good answer. You should have asked that has a comment, where you could have jokingly answered to `return "com.mycomp.war.tasks.JmxMetricsTask"` if you wanted to introduce your point. – Aaron Apr 28 '17 at 13:42
  • Yes, but in Stackoverflow you can only comment on question when your reputation score is greater than 50, but (un)fortunately mine is 23 :) – Afridi Apr 28 '17 at 13:57
-1

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
    }
}
Community
  • 1
  • 1
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • Why the downvote, was it because I left out the case-insensitive check? Well, it's fixed. Thanks for leaving feedback? :) – Mr. Polywhirl Apr 28 '17 at 13:12