我有一个文本文件,如下所示:
[* content I want *]
[ more content ]
我想阅读该文件并能够提取content I want
。我能做的最好的是下面但是它会返回
[ more content ]
请注意,content I want
和more content
都包含括号和括号,但它们从不包含[*
或*]
。
public static String parseFile(String src) throws IOException
{
String s = "";
File f = new File(src);
Scanner sc = new Scanner(f);
sc.useDelimiter("\\[\\*([^]]+)\\*\\]");
s= sc.next();
sc.close();
return s;
}
答案 0 :(得分:3)
以下正则表达式应该有效:
\[\s*\*\s*(.*?)\s*?\*\s*\]
https://regex101.com/r/uC4lH9/3
您可以像这样使用它(Java 8):
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexExample {
public static final Pattern PATTERN = Pattern.compile("\\[\\s*\\*\\s*(.*?)\\s*?\\*\\s*\\]");
public static List<String> parse(String fileContent) {
Matcher matcher = PATTERN.matcher(fileContent);
List<String> foundData = new ArrayList<>();
while (matcher.find()) {
foundData.add(matcher.group(1));
}
return foundData;
}
public static void printOutList(List<? extends CharSequence> list) {
list.forEach(System.out::println);
}
public static void main(String[] args) {
printOutList(parse("[ this will not match ] [ * YOU WILL BE MATCHED!!!11 * ] [* you as well *] [*you too*]" +
" [ * this as well *] [this * will * not]"));
}
}
输出:
YOU WILL BE MATCHED!!!11
you as well
you too
this as well