Solved

Java: Regular expression failing, any thoughts?

Posted on 2012-04-05
3
251 Views
Last Modified: 2012-06-22
Hello Experts,

I am not sure how to set up group(3).... any ideas?

	public static void main(String[] args) {
		// log source
		String source = "12:22:22.826 error 009f01c1a3bdb079 \n"  +
				"12:22:22.926 message 007v01c1a3bdr089 \n" +
				"12:22:23.126 error 001f01p1a3bwrb079 \n" +
				"12:22:24.026 error 001f01p1a3bwrtr079 \n"  +
				"";		
		Pattern qPattern = Pattern.compile("(\\d{2}:\\d{2}:\\d{2}\\.\\d{3}\\s).*(error).*(\\s\\W+\\s)"); //this also works	
		Matcher qMatcher = qPattern.matcher(source);
		while (qMatcher.find()) {
			//test
			System.out.println("groups: " + qMatcher.groupCount());	
			System.out.println("error: " + qMatcher.group(3));
		}
	}// END OF MAIN

Open in new window


in advance, thank you for all (or any) help...

regards,
0
Comment
Question by:epifanio67
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 37814586
\\W tells it to match a non-word character - you need the reverse.

Also, you need (if you're simulating log file lines) to use a Scanner on the String you have:

   public static void main(String[] args) {
        // log source
        String source = "12:22:22.826 error 009f01c1a3bdb079 \n" +
            "12:22:22.926 message 007v01c1a3bdr089 \n" +
            "12:22:23.126 error 001f01p1a3bwrb079 \n" +
            "12:22:24.026 error 001f01p1a3bwrtr079 \n" + "";

        Pattern qPattern = Pattern.compile(
                "(\\d{2}:\\d{2}:\\d{2}\\.\\d{3}\\s).*?(error).*?(\\s\\w+\\s)"); //this also works	
        Scanner s = new Scanner(source);

        while (s.hasNextLine()) {
            String line = s.nextLine();
            Matcher qMatcher = qPattern.matcher(line);

            while (qMatcher.find()) {
                //test
                System.out.println("groups: " + qMatcher.groupCount());
                System.out.println("error: " + qMatcher.group(3));
            }
        }

        s.close();
    } // END OF MAIN
 

Open in new window

0
 

Author Closing Comment

by:epifanio67
ID: 37816624
thank you so much for your help...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37817155
:)
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javap not working 8 46
Configure a Bean in an XML file 4 32
by zero exception 10 41
difference between sorce folder and folder in eclipise 3 14
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question