[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Java: Regular expression failing, any thoughts?

Posted on 2012-04-05
3
Medium Priority
?
259 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month17 days, 22 hours left to enroll

831 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