Solved

Java: Regular expression, how to?

Posted on 2012-04-02
5
262 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
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

Title # Comments Views Activity
how to add new optional parameter to JSP 1 49
null output 3 35
ejb on wildfly 5 30
How to determine if a string is a valid SHA value 7 35
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 …
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

828 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