Solved

Java: Regular expression failing, any thoughts?

Posted on 2012-04-05
3
248 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now