[英]How to extract content between two words in a text file using Java?
I have a text file and I want to extract the content between these two words using Java . 我有一个文本文件,我想使用Java提取这两个单词之间的内容。
I am new to Java ,can anyone help me out ? 我是Java新手,有人可以帮助我吗?
This is the method in R language to extract content between words Directions & Ingredients . 这是R语言中用于提取单词Directions&Ingredients之间的内容的方法。
sub("."*Directions*(.*?)* Photograph.*,\\1",x)
where x
is the text content . 其中
x
是文本内容。 Can anyone tell me the corresponding code in Java
. 谁能告诉我相应的
Java
代码。
Thanks 谢谢
If you are allowed to use apache-commons ; 如果允许使用apache-commons; this can be elegantly done as :
可以这样优雅地完成:
String[] results = StringUtils.substringsBetween(str,"Directions","Photograph");
Where str
being the string in question. 其中
str
是有问题的字符串。 Dependency you need is commons-lang-XXX.jar
您需要的依赖关系是
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.