Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to match against multiple regular expressions and determine which expression matched in Java
    primarykey
    data
    text
    <p>I've got a Java program that goes through a file line by line, and tries to match each line against one of four regular expressions. Depending on which expression matched, the program performs a certain action. Here's what I have:</p> <pre><code>private void processFile(ArrayList&lt;String&gt; lines) { ArrayList&lt;Component&gt; Components = new ArrayList&lt;&gt;(); Pattern pattern = Pattern.compile( "Object name\\.{7}: (.++)|" + "\\{CAT=([^\\}]++)\\}|" + "\\{CODE=([^\\}]++)\\}|" + "\\{DESC=([^\\}]++)\\}"); Matcher matcher; // Go through each line and see if the line matches the any of the regexes // defined Component currentComponent = null; for (String line : lines) { matcher = pattern.matcher(line); if (matcher.find()) { // We found a tag. Find out which one String match = matcher.group(); if (match.startsWith("Obj")) { // We've got the object name 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); } } </code></pre> <p>As you can see, I've combined the four regular expressions into one and apply the whole regex on the line. If a match is found, I check the start of the string to determine which expression was matched and then extract the data from the group. In case anyone is interested in running the code, some sample data is presented below:</p> <pre><code>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} </code></pre> <p>Although my code works, I don't like the fact that I repeat part of the string later on when calling the startsWith routines.</p> <p>I'm curious to see how others would have written this.</p> <p>Amr</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload