Solved

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

Posted on 2012-03-28
5
301 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

757 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

21 Experts available now in Live!

Get 1:1 Help Now