Solved

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

Posted on 2012-03-28
5
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…
Suggested Courses

636 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