Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java: Regular expression failing, any thoughts?

Posted on 2012-04-05
3
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses

609 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