Solved

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

Posted on 2012-04-12
3
240 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
[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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HashTable highest marks enumeration alternative 9 48
Desingning Refactoring existing code 2 31
Setup GlassFish 4 36
Java array 10 63
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…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

726 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