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

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

epifanio67Asked:
Who is Participating?
 
CEHJCommented:
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
 
CEHJCommented:
Please rephrase your question. What is your goal?

http://technojeeves.com/joomla/index.php/free/117-smart-questions
0
 
epifanio67Author Commented:
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
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
CEHJCommented:
I want to do a search between the endIndex and lastIndex....

A search for what?
0
 
epifanio67Author Commented:
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
 
epifanio67Author Commented:
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
 
epifanio67Author Commented:
Thank you CEHJ for your help....
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.