Solved

Java: regular expression to filter times in order, how to?

Posted on 2012-03-28
5
305 Views
Last Modified: 2012-03-28
Hello Experts,

The regular expression below is not working for me... I want to filter times and order them...

any thoughts?

thx for your help...

regards,

public class RegularExpression {

	public static void main(String[] args) {
		// log source
		String source = "12:22:22.926_I_I_00 [07:48] function will be continued(0,832569404) \n" +
				"12:22:22.726_I_I_00a [09:04] <<<<<<<<<<<<suspend interpretator(JUMPING), timers:0000 \n" +
				"12:22:22.827_I_I_0 [07:49] function is continued(0,832569404) \n" +
				"12:22:22.828_I_I_00a101c1a3bb4073 [09:06] >>>>>>>>>>>>start interpretator \n " +
				"12:22:22.829 Int 22000  \n" +
				"12:22:22.830_I_I_00a1 [07:38] HERE IS XDATA \n" +
				" 12:22:22.831_I_I_0 [09:04] <<<<<<<<<<<<suspend interpretator(XDATA_EX), timers:00010 \n" +
				" 12:22:22.841_T_I_0000000000000000 [14:0c]  (this dn=Preroute) \n" +
				" 12:22:22.841_I_I_0000000000000000 [01:01] call (2984793-036846a0) for Resources created (del 490) \n" +
				" 12:22:22.931_I_I_0 [09:04] <<<<<<<<<<<<suspend interpretator(XDATA_EX), timers:00010 \n" +
				" 12:22:23.141_T_I_0000000000000000 [14:0c]  (this dn=Preroute) \n" +
				" 12:22:23.841_I_I_0000000000000000 [01:01] call (2984793-036846a0) for Resources created (del 490) \n" +
				"";

//		Vector<String> times = new Vector<String>();
		Pattern timePattern = Pattern.compile("[\\dd]*(\\p{Punct})*[\\dd]*(\\p{Punct})*[\\dd]*(\\p{Punct})*[\\ddd]");
		Matcher timeMatcher = timePattern.matcher(source);
		while (timeMatcher.find()) {
			System.out.println(timeMatcher.group());
//			times.add(m.group());
		}
		


	}// END OF MAIN

}// END OF CLASS

Open in new window

0
Comment
Question by:epifanio67
5 Comments
 
LVL 35

Accepted Solution

by:
Terry Woods earned 250 total points
ID: 37779068
Try this:
Pattern timePattern = Pattern.compile("\\d+:\\d+:\\d+\\.*\\d+");
0
 
LVL 47

Expert Comment

by:for_yan
ID: 37779082
But here you have each line starting with time.
You don't need regex for that make it
ByteaArrayInputStream(source.getBytes())
and then read lnes, trim() them and then so substring (0, 12) and parse it to time
using SimpleDateFormat

What do you want as the output ?
0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 250 total points
ID: 37779106
I think this should be more accurate, if you still want regex:

Pattern.compile("\\d{2}:\\d{2}:\\d{2}\\.\\d{3}");
0
 

Author Closing Comment

by:epifanio67
ID: 37779147
thank you experts... both regular expressions work...
for_yan, I am also going to try ByteaArrayInputStream(source.getBytes())....

thx a million...

regards
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 37779169
for_yan, I am also going to try ByteaArrayInputStream(source.getBytes())....

Don't bother - use a Scanner ;)
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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
servlet filter example 37 82
Java exception bubble up 2 24
Which non-HTML GUI front end to use with Java? 3 50
Eclipse for Java EE development 2 43
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

685 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