import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:\\b\\w++::\\w++\\s*+\\(|\\G(?<!^))\\s*+(\\w++)\\s*+(?:\\[[^]]*+]\\s*+)?,";
final String string = "Not even sure how to attack this Regex Need (Multiline text with extraction of library names)\n\n"
+ "Sample Text\n\n"
+ " box::use(\n"
+ " DBI[dbListTables, dbExecute],\n"
+ " Yessir[this_one, that one,\n"
+ " and_this_one],\n"
+ " Maybesir[\n"
+ " func_one,\n"
+ " func_two,\n"
+ " ],\n"
+ " Nosir,\n"
+ " \n"
+ " database = logic/database,\n"
+ " log = logic/log,\n"
+ " options = logic/options,\n"
+ " utilities = logic/utilities,\n"
+ " )\n\n"
+ "I would like to have a regexp which matches the following from the above text:\n\n"
+ " DBI, Yessir, Maybesir, Nosir\n\n"
+ "Is there an easy way to approach this? I have been trying to use the regexp101 website to help me out here, but this one is sufficiently complex that I am a bit out of my depth. My current line is the following:\n\n"
+ " box::use\\(\\n(?:[\\s]*([A-Za-z0-9]*)(?:[A-Za-z0-9\\[\\]_\\ ,]*\\n))\n\n"
+ "But, this is of course not getting it. I am not sure how to handle getting the multiple (unknown how many there really would be) libraries inside the box::use function.\n\n"
+ "It might be easier to extract the text from inside the use::box function first and then regexp that?\n\n"
+ " \n"
+ "Edit: Forgot to add that I am using Python3";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html