Java - 在[* ... *]之间提取所有内容

时间:2015-08-20 18:30:08

标签: java regex parsing

我有一个文本文件,如下所示:

[* content I want *] 
[ more content ]

我想阅读该文件并能够提取content I want。我能做的最好的是下面但是它会返回

[ more content ]

请注意,content I wantmore 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;
}

1 个答案:

答案 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

自己动手:https://ideone.com/ldclWA