tags:

views:

338

answers:

4

Hi all,

I want to get all text in between 2 words wherver it is. For example:

String Testing="one i am here fine two one hope your are also fine two one ok see you two";

From the above string, I want to fetch the words between "one" and "two" in array:

My result should be stored in array like this:

String result[1] = i am here fine
String result[2] = hope your are also fine  
String result[3] = ok see you

How to do in java?

Thanks in advance

  • Gnaniyar Zubair
+4  A: 
String input = "one i am here fine two one hope your are also fine two one ok see you two;";
Pattern p = Pattern.compile("(?<=\\bone\\b).*?(?=\\btwo\\b)");
Matcher m = p.matcher(input);
List<String> matches = new ArrayList<String>();
while (m.find()) {
  matches.add(m.group());
}

This will create a List of all the text between "one" and "two".

If you want a simpler version that doesn't use lookaheads/lookbehinds try:

String input = "one i am here fine two one hope your are also fine two one ok see you two;";
Pattern p = Pattern.compile("(\\bone\\b)(.*?)(\\btwo\\b)");
Matcher m = p.matcher(input);
List<String> matches = new ArrayList<String>();
while (m.find()) {
  matches.add(m.group(2));
}

Note: Java arrays are zero-based not one-based so in your example the first result would be in result[0] not result[1]. In my solution, the first match is in matches.get(0).

cletus
+1 Second regex is better, no need to use lookaround, especially lookbehind, unless you need to, it's an efficiency killer!
Paul Creasey
HI cletus, amazing thanks for your immediate response. thanks a lot.
Gnaniyar Zubair
I tried like this:instead of "one", my starting values is :/c/document_library/get_file?end value is ( instead of two): Matcher m = p.matcher(content); List<String> matches = new ArrayList<String>(); List<String> result = new ArrayList<String>();while (m.find()) { matches.add(m.group()); } for(int z=0; z<matches.size(); z++) { result.add(matches.get(z));} System.out.println("result1 >>> = " + matches.get(0));But i am getting Index =0 ; size = 0....
Gnaniyar Zubair
A: 

Just use indexOf and subString Methods

Chathuranga Chandrasekara
+1  A: 

Check out the Java Pattern class that allows you to use regular expressions to identify substrings and consequently split up a larger string. You need something like

Pattern.compile("\bone\B");

to identify 'one'. The \b and \B are word boundary matches. You need these so you don't accidentally match on "someone" rather than the word "one" (as an aside, I'd recommend a different delimiter rather than the words "one", "two" etc.)

Brian Agnew
A: 

I tried like this: instead of "one", my starting values is :/c/document_library/get_file?

end value is ( instead of two): &groupId=

Pattern p = Pattern.compile("(\b/c/document_library/get_file?uuid=\b)(.*?)(\b&groupId=\b)");

Matcher m = p.matcher(content);

List matches = new ArrayList();

List result = new ArrayList();

while (m.find())

{

matches.add(m.group());

}

for(int z=0; z>> = " + matches.get(0));

But i am getting Index =0 ; size = 0....

Gnaniyar Zubair