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
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
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.