Solved

Java: how to get the line number of the pattern matched?

Posted on 2012-04-12
3
232 Views
Last Modified: 2012-04-12
Hello Experts,

is there a way to get the line number where the pattern was matched?

	public static void main(String[] args) {

		String source = "12:22:22.826 message QueueRoute to 5554443322 \n"
				+ "	response callerAttributes id:5554443322 \n"
				+ "	confirmation callDelivered to agent 675 \n"
				+ "12:22:22.926 message AddRoute to queue 65 \n"
				+ "12:22:23.126 message CancelRoute to queue 45 \n"
				+ "12:22:24.026 message QueueRoute to 3332227766 \n"
				+ "	response callerAttributes id:5558883322 \n"
				+ "	confirmation callDelivered to agent 675 \n" +
				"";
		Pattern p = Pattern.compile("(\\d{2}:\\d{2}:\\d{2}\\.\\d{3}).*(Queue\\w+).*(\\s\\d{10}\\s)"); 
		Matcher m = p.matcher(source);
		while (m.find()) {
			// test
			System.out.println("Time: " + m.group(1));
			System.out.println("call: " + m.group(2) + m.group(3));

		}

	}// end of main

Open in new window


in other words, groups 1, 2, and 3 were found @ line 1 and 6...

see what I mean? any ideas how to do this?

in advance, thank you for any help provided...

regards,
0
Comment
Question by:epifanio67
  • 2
3 Comments
 
LVL 20

Accepted Solution

by:
TheAvenger earned 500 total points
ID: 37840169
Would it be an acceptable solution for you to split the source into lines and then use a for loop to check each line separately? Then you could use the parameter of the for loop as your line counter.
0
 

Author Comment

by:epifanio67
ID: 37840437
let me try that... thx for the suggestion..
0
 

Author Closing Comment

by:epifanio67
ID: 37840597
thank you expert...
0

Featured Post

Free Trending Threat Insights Every Day

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

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)
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…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

758 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

22 Experts available now in Live!

Get 1:1 Help Now