mv3
mv3

Reputation: 469

Java - Extract everything between [* ... *]

I have a text file which looks like this:

[* content I want *] 
[ more content ]

I would like to read the file and be able to extract content I want. The best I could do is below but it returns

[ more content ]

Please note that both content I want and more content contain brackets and parenthesis but they never contain [* or *].

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;
}

Upvotes: 0

Views: 76

Answers (1)

randers
randers

Reputation: 5146

The following regular expression should work:

\[\s*\*\s*(.*?)\s*?\*\s*\]

https://regex101.com/r/uC4lH9/3

You can use it like this (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]"));
}
}

Output:

YOU WILL BE MATCHED!!!11
you as well
you too
this as well

See it yourself: https://ideone.com/ldclWA

Upvotes: 3

Related Questions