Solved

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

Posted on 2012-04-12
3
236 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
numbers ascending pyramid 101 191
stackato and cloud 4 85
Spring Framework HTTPSession management 1 33
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

805 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