I have a Java program that goes through line by line and tries to match each line with one of four regular expressions. Depending on which expression matches, the program performs a specific action. Here is what I have:
private void processFile(ArrayList<String> lines) {
ArrayList<Component> Components = new ArrayList<>();
Pattern pattern = Pattern.compile(
"Object name\\.{7}: (.++)|"
+ "\\{CAT=([^\\}]++)\\}|"
+ "\\{CODE=([^\\}]++)\\}|"
+ "\\{DESC=([^\\}]++)\\}");
Matcher matcher;
Component currentComponent = null;
for (String line : lines) {
matcher = pattern.matcher(line);
if (matcher.find()) {
String match = matcher.group();
if (match.startsWith("Obj")) {
if (currentComponent != null) {
Components.add(currentComponent);
}
currentComponent = new Component();
currentComponent.setName(matcher.group(1));
} else if (currentComponent != null) {
if (match.startsWith("{CAT")) {
currentComponent.setCategory(matcher.group(2));
} else if (match.startsWith("{CODE")) {
currentComponent.setOrderCode(matcher.group(3));
} else if (match.startsWith("{DESC")) {
currentComponent.setDescription(matcher.group(4));
}
}
}
}
if (currentComponent != null) {
Components.add(currentComponent);
}
}
As you can see, I combined the four regular expressions into one and applied the entire regular expression in the string. If a match is found, I check the beginning of the line to determine which expression was matched, and then retrieve the data from the group. In case someone is interested in running the code, some examples of the data are presented below:
Object name.......: PMF3800SN
Last modified.....: Wednesday 9 November 2011 11:55:04 AM
File offset (hex).: 00140598 (Hex).
Checksum (hex)....: C1C0 (Hex).
Size (bytes)......: 1,736
Properties........: {*DEVICE}
{PREFIX=Q}
{*PROPDEFS}
{PACKAGE="PCB Package",PACKAGE,1,SOT-323 MOSFET}
{*INDEX}
{CAT=Transistors}
{SUBCAT=MOSFET}
{MFR=NXP}
{DESC=N-channel TrenchMOS standard level FET with ESD protection}
{CODE=1894711}
{*COMPONENT}
{PACKAGE=SOT-323 MOSFET}
*PINOUT SOT-323 MOSFET
{ELEMENTS=1}
{PIN "D" = D}
{PIN "G" = G}
{PIN "S" = S}
, , startsWith.
, .