Solved

Java: Regular expression, how to?

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
configure dependency in POM for new database 3 25
tomcat startup error 5 59
runtime exception 2 27
null output 3 24
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

815 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

11 Experts available now in Live!

Get 1:1 Help Now