import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:\\s*?NAME\\s*?:\\s*?\\\"?)([0-9A-Za-z- ,]+)(?:\\\"?\\s*?,\\s*?)(?:DESCR\\s*?:\\s*\\\"?)([0-9A-Za-z- ,/+:]+)(?:\\\"?\\s*?PID\\s*?:\\s*?)([0-9A-Z-]+)(?:\\s*?,\\s*?VID\\s*?:\\s*?)([0-9A-Z/]+)(?:\\s*?,\\s*?SN\\s*?:\\s*?)([0-9A-Z/]+)";
final String string = "NAME: \"Chassis\", DESCR: \"Nexus C9348GC-FXP chassis\"\n"
+ "PID: N9K-C9348GC-FXP , VID: V01 , SN: FFFFFFFFFFF\n\n"
+ "NAME: \"Slot 1 \", DESCR: \"48x100M/1G \"\n"
+ "PID: N9K-C9348GC-FXP , VID: V01 , SN: FFFFFFFFFFF\n\n"
+ "NAME: \"GEM \", DESCR: \"4x25G+2x100G Switch \"\n"
+ "PID: N9K-C9348GC-FXP , VID: V01 , SN: FFFFFFFFFFF\n\n"
+ "NAME: \"power Supply 1\", DESCR: \"PSU \"\n"
+ "PID: NXA-PAC-350W-PE , VID: V01 , SN: DDDDDDDDDDD\n\n"
+ "NAME: \"power Supply 2\", DESCR: \"PSU \"\n"
+ "PID: NXA-PAC-350W-PE , VID: V01 , SN: DDDDDDDDDDD\n\n"
+ "NAME: \"Fan 1 \", DESCR: \"fan \"\n"
+ "PID: NXA-FAN-30CFM-F , VID: N/A , SN: N/A\n\n"
+ "NAME: \"Fan 2 \", DESCR: \"fan \"\n"
+ "PID: NXA-FAN-30CFM-F , VID: N/A , SN: N/A\n\n"
+ "NAME: \"Fan 3 \", DESCR: \"fan \"\n"
+ "PID: NXA-FAN-30CFM-F , VID: N/A , SN: N/A";
final String subst = "";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
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