Solved

Java: Regular expression, how to?

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights2  challenge 7 76
countX 22 70
Java - Why doesn't this JFrame work 3 18
GUI builder for Eclipse? 8 11
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now