import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:\\[device name\\]:\\[?|\\[name_of_device\\]- |,)([^\\],\\r\\n]*)(?=.*\\r?\\n(?:[ \\t]*\\[.*\\r?\\n)*?[ \\t]*\\[(?:current software\\]: |software_running\\]- )(.*))";
final String string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. This is a list of all kinds of models, software, and release dates. See below for more details\n"
+ "{\n"
+ " #device batch 1 info can be found below\n"
+ " {\n"
+ " [device name]:device_1\n"
+ " [initial software]: SW$1_978qhuhk\n"
+ " [current software]: sW98203_973\n"
+ " [date] : 2022-01-05\n"
+ " }\n"
+ " {\n"
+ " [device name]:[device_2, device_3]\n"
+ " [initial software]: sW$3_jghs\n"
+ " [current software]: sW$3_jghs2\n"
+ " [date] : 2022-07-03\n"
+ " }\n"
+ "#wifi devices can be found here\n"
+ " {\n"
+ " {\n"
+ " \"nested children\":\n"
+ " [device name]:[w_device_1, w_device_pro]\n"
+ " [initial software]: 982637012_sw\n"
+ " [current software]: 93834932_swp$\n"
+ " [date] : 2024-02-01\n"
+ " }\n"
+ " {\n"
+ " [name_of_device]- mesh_plus\n"
+ " [installation_date]-2023-03-21\n"
+ " [software_running]- mesh_sw_old\n"
+ " }\n"
+ " }\n"
+ "}\n";
final String subst = "";
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
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