0

I have a text file and I want to extract the content between these two words using Java .

I am new to Java ,can anyone help me out ?

This is the method in R language to extract content between words Directions & Ingredients .

   sub("."*Directions*(.*?)* Photograph.*,\\1",x)

where x is the text content . Can anyone tell me the corresponding code in Java .

Thanks

akrun
  • 874,273
  • 37
  • 540
  • 662
user4599
  • 95
  • 1
  • 1
  • 9
  • May be this link helps http://stackoverflow.com/questions/16597303/extract-string-between-two-strings-in-java – akrun Jun 05 '15 at 08:25
  • 1
    I wouldn't really tag this with R since it's purely a Java question. Look up "regular expressions in java". You should be able to easily translate the regex you have in R to one in Java if you just look up the syntax – DeanAttali Jun 05 '15 at 08:52

3 Answers3

3

Use Java Pattern class:

String x = new String("string that contains Directions and Photograph. ");
Pattern pattern = Pattern.compile("Directions(.*?) Photograph");
Matcher matcher = pattern.matcher(x); 
while (matcher.find()) {
System.out.println(matcher.group(1));
}

Live DEMO

54l3d
  • 3,913
  • 4
  • 32
  • 58
1

If you are allowed to use apache-commons ; this can be elegantly done as :

String[] results =  StringUtils.substringsBetween(str,"Directions","Photograph");

Where str being the string in question. Dependency you need is commons-lang-XXX.jar

ring bearer
  • 20,383
  • 7
  • 59
  • 72
0

Thes simplest way is:

    String originalString = "bla, bla, bla, Directions bla ... bla ... bla.... Ingredients ....";
    String result = originalString.substring(originalString.indexOf("Directions")+"Directions".length(), originalString.indexOf("Ingredients"));
    System.out.println(result);
robcalvo
  • 11
  • 1
  • Using patterns is better as it states the intent clearly. Also, `substring()` when used with large strings (for example, processing a batch file with millions of lines) can lead to unexpected heap issues(a bug fixed in Java1.7) – ring bearer Jun 12 '15 at 08:25