?
Solved

Java: is there a way to search for string or pattern between endIndex and lastIndex?

Posted on 2012-09-04
8
Medium Priority
?
321 Views
Last Modified: 2012-09-04
Hello Experts,

The code below indexes the keyword id.

	public static void main(String[] args) {

		String file = "message 5896ut45it SipRoute with header SipHead1 in SipUser " + "\n"
				+ "receipt SipCall at time SipUpdate 5896ut45it" + "\n"
				+ "5896ut45it message SipReg during SipDiv 5896ut45it " + "\n"
				+ "receipt SipCall at time SipUpdate " + "\n";
		String id = "5896ut45it";
		
		int firstOffset = -1;
		int lastIndex = 0;
		int keyWordSize = id.length(); 

		while ((lastIndex = file.indexOf(id, lastIndex)) != -1) {

			int endIndex = lastIndex + keyWordSize;

			System.out.println("lastIndex: " + lastIndex + " endIndex: " + endIndex);			
			
			if (firstOffset == -1) 	firstOffset = lastIndex;
			
			lastIndex = endIndex;
		}

	}// end of main

Open in new window


these are the indexes found:

lastIndex: 8 endIndex: 18
lastIndex: 95 endIndex: 105
lastIndex: 106 endIndex: 116
lastIndex: 146 endIndex: 156

How can search for "Sip\\w*" between indexes?
In other words, how can I search for "Sip\\w*" only between the endIndex and lastIndex?
Or only between 18 and 95, 116 and 146....

See what I mean?

In advance, thanks for your help....
	public static void main(String[] args) {

		String file = "message 5896ut45it SipRoute with header SipHead1 in SipUser " + "\n"
				+ "receipt SipCall at time SipUpdate 5896ut45it" + "\n"
				+ "5896ut45it message SipReg during SipDiv 5896ut45it " + "\n"
				+ "receipt SipCall at time SipUpdate " + "\n";
		String id = "5896ut45it";
		
		int firstOffset = -1;
		int lastIndex = 0;
		int keyWordSize = id.length(); //size of connId = 16

		while ((lastIndex = file.indexOf(id, lastIndex)) != -1) {

			int endIndex = lastIndex + keyWordSize;

			System.out.println("lastIndex: " + lastIndex + " endIndex: " + endIndex);			
			
			if (firstOffset == -1) 	firstOffset = lastIndex;
			
			lastIndex = endIndex;
		}

	}// end of main

Open in new window

0
Comment
Question by:epifanio67
  • 4
  • 4
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 38364853
Please rephrase your question. What is your goal?

http://technojeeves.com/joomla/index.php/free/117-smart-questions
0
 

Author Comment

by:epifanio67
ID: 38364898
sorry CEHJ..

I want to do a search between the endIndex and lastIndex....
the file / logs I am parsing must be searched between each id found...

or

another way to explain it would be:
do a search between each id found....

hopefully that explains my thoughts better...

Thanks for your help...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38365224
I want to do a search between the endIndex and lastIndex....

A search for what?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:epifanio67
ID: 38365482
oh man... so sorry...

in between the endIndex and lastIndex, I want to search for
String sip = "Sip\\w*";

thanks so much for your help...
Regards,
0
 

Author Comment

by:epifanio67
ID: 38365563
Hello experts,

I just updated the code to give you a better idea what I am trying to do:

	public static void main(String[] args) {

		String file = "message 5896ut45it SipRoute with header SipHead1 in SipUser " + "\n"
				+ "receipt SipCall at time SipUpdate 5896ut45it" + "\n"
				+ "5666ut32it message SipReg during SipDiv 5666ut32it " + "\n"
				+ "receipt SipCall at time SipUpdate " + "\n";
		String id = "5896ut45it";
		ArrayList<String> list = null;
		
		int idFirstOffset = -1;
		int idLastIndex = 0;
		int idKeyWordSize = id.length(); 

		while ((idLastIndex = file.indexOf(id, idLastIndex)) != -1) {

			int idEndIndex = idLastIndex + idKeyWordSize;

			System.out.println("idlastIndex: " + idLastIndex + " idendIndex: " + idEndIndex);			
			Pattern p = Pattern.compile("Sip\\w*");
			Matcher matcher = p.matcher(file);
			list = new ArrayList<String>();
			while(matcher.region(idEndIndex, idLastIndex) != null){
		    	list.add(matcher.group());		    	
		    }
			
			if (idFirstOffset == -1) idFirstOffset = idLastIndex;
			
			idLastIndex = idEndIndex;
		}
		
		//prints arraylist ok
		for (int x = 0; x < list.size(); x++) {
			System.out.println("events: " + list.get(x));
		}

	}// end of main

Open in new window


for some reason it is not working...

Thanks for your help...
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 38365701
Try the following, obviously capturing into a collection if required:
    static void ez() {
        String file = "message 5896ut45it SipRoute with header SipHead1 in SipUser " +
            "\n" + "receipt SipCall at time SipUpdate 5896ut45it" + "\n" +
            "5666ut32it message SipReg during SipDiv 5666ut32it " + "\n" +
            "receipt SipCall at time SipUpdate " + "\n";
        String id = "5896ut45it";
	Pattern p = Pattern.compile("(Sip\\w*)");
	for(String s : file.split(id)) {

	    Matcher m = p.matcher(s);
	    while(m.find()) {
		System.out.println(m.group(1));
	    }
	}
    }

Open in new window

0
 

Author Closing Comment

by:epifanio67
ID: 38365764
Thank you CEHJ for your help....
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 38365835
:)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Don’ts and Dos are two important end products of software testing basics that a tester needs to regard. This article attempts to explain the principles of both.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…
Suggested Courses
Course of the Month8 days, 7 hours left to enroll

616 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