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
Solved

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

Posted on 2012-04-12
3
239 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple java question 3 59
session migration servlets 2 42
servlet example 17 51
Java syntax, or is it Selenium 6 30
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
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:

856 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