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

x
?
Solved

Java: Regular expression, how to?

Posted on 2012-04-02
5
Medium Priority
?
273 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 36

Assisted Solution

by:mccarl
mccarl earned 1000 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 1000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

610 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