Solved

Java: Regular expression, how to?

Posted on 2012-04-02
5
263 Views
Last Modified: 2012-04-03
Hello Experts,

can a regular expression locate two patterns in the same line? see code below

public class RegularExpression {

	public static void main(String[] args) {
		// log source
		String source = "12:22:22.826 message QueueRoute to 5554443322 \n"  +
				"12:22:22.926 message AddRoute to queue 65 \n" +
				"12:22:23.126 message CancelRoute to queue 45 \n" +
				"";		

		Pattern timePattern = Pattern.compile("\\d{2}:\\d{2}:\\d{2}\\.\\d{3} && (Queue\\w+)"); //this also works		
		Matcher timeMatcher = timePattern.matcher(source);
		while (timeMatcher.find()) {
			System.out.println(timeMatcher.group());
		}

	}// END OF MAIN

}// END OF CLASS

Open in new window


in other words, if time and word found, do something...
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
5 Comments
 
LVL 11

Expert Comment

by:techhealth
ID: 37798799
Why not just define two Patterns and check if both (or either) of them match?
0
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 250 total points
ID: 37798801
public class RegularExpression {

	public static void main(String[] args) {
		// log source
		String source = "12:22:22.826 message QueueRoute to 5554443322 \n"  +
				"12:22:22.926 message AddRoute to queue 65 \n" +
				"12:22:23.126 message CancelRoute to queue 45 \n" +
				"";		

		Pattern timePattern = Pattern.compile("(\\d{2}:\\d{2}:\\d{2}\\.\\d{3}).*(Queue\\w+)"); //this also works		
		Matcher timeMatcher = timePattern.matcher(source);
		while (timeMatcher.matches()) {
			System.out.println("Time: " + timeMatcher.group(1));
			System.out.println("Word: " + timeMatcher.group(2));
		}

	}// END OF MAIN

}// END OF CLASS

Open in new window


It has to find a timestamp followed by any number of characters and then the word Queue with at least one 'word' character after it. And then it prints the time stamp and the Queue word.
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 250 total points
ID: 37798803
Sure, have them separated by .* (i.e., zero or more characters)


something like:

Pattern timePattern = Pattern.compile("\\d{2}:\\d{2}:\\d{2}\\.\\d{3}.*(Queue\\w+)");
0
 

Author Closing Comment

by:epifanio67
ID: 37798811
thank you experts...
I really appreciate all of your help...

regards,
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 37800273
Thanks for the grade & points

Good luck & have a great day.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
xampp tool 12 58
how to see all occupied ports on windows 10 laptop 15 83
program arguments vs VM arguments 4 28
Java array 21 81
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

756 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