?
Solved

Java: Regular expression failing, any thoughts?

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
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:
Suggested Courses
Course of the Month12 days, 4 hours left to enroll

752 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