javaregexmultilinealternation

Troubles with Java regex and alternation


I'm having troubles getting a regex to work. I'm trying to parse a large, multiline block of text for certain XML tags. The reason I'm not parsing this with an XML library however is it's actually part of a block of ESQL as well. The line I'm using is as follows:

Pattern.compile(".*'(Invoice|Package|Mapping|Post)' AS STAGE.*(<(ESQL|ProcessInvoice)>.+)</(ESQL|ProcessInvoice)>).*", Pattern.DOTALL);

My problem is actually two fold:

  1. The (Invoice|Package|Mapping|Post) section only matches to Invoice, unless I remove Invoice from the list. Then it matches to only Mapping. What sticks me as odd is that Package is in the middle of the text block (the blocks are ordered Invoice, Package, Mapping, Post in the text file, with Post being optional so it may not even be there) and mapping is toward the end.

  2. The <(ESQL|ProcessInvoice)> section actually takes ProcessInvoice block (the very last block, past three <ESQL> blocks at the end). If I remove the (ESQL|ProcessInvoice) part and just make it <ESQL> it will, oddly, take the Package block, again, rather than the first block for the Invoice. This continues to be a problem even if I pare this down to only be one of the four sections from before (so, just Invoice) with no alternation anywhere. It will skip past the first section and take the second one.

I admit I'm no regex guru, but this seems like rather odd behaviour. Calling .reset() on the Matcher doesn't make it recognize an earlier block either, and .find() only locates one match instead of iterating through all the possible matches.

---Addendum--- Example input as follows (edited for content):

CREATE COMPUTE MODULE Module_Name
CREATE FUNCTION Main() RETURNS BOOLEAN
BEGIN       
    Header stuff,
    'Invoice' AS STAGE,
    Gibberish here
    '<Rule>
    <ESQL>
        ESQL Block 1
    </ESQL>
    <ESQL>
        ESQL Block 2
    </ESQL> 
    </Rule>' AS CONTENT);

    Header stuff,
    'Package' AS STAGE,
    Gibberish here
    '<Rule>
    <ESQL>
        ESQL Block 3
    </ESQL>
    </Rule>' AS CONTENT);

    Header stuff as well,
    'Mapping' AS STAGE,
    Gibberish here too
    '<ProcessInvoice>
        Another ESQL Block
    </ProcessInvoice>' AS CONTENT);
END;
END MODULE;

The intended groupings should be (respectively):

  1. Invoice
  2. Package
  3. Mapping

And data:

  1. ESQL Block 1 ESQL Block 2
  2. ESQL Block 3
  3. Another ESQL Block

I should mention I altered my regex slightly now to account for , and it is now as follows:

.*?'(Package|Invoice|Post)' AS STAGE.*?<Rule>(.+?)</Rule>.*?

This alternation seems to work now for three of the four possible sections, but I believe part of my earlier trouble was trying to use <(ESQL|ProcessInvoice)> inside of another group. Trying to do without <Rule>(.+?)</Rule>.*? and instead do even just (<ESQL>.+?</ESQL>) doesn't want to work now.


Solution

  • I'd change .* to .*? to make it non-greedy. That will probably help you.

    But really you'd be better using an XML parser. You say you can't use an XML parser because the XML is embedded inside other text. Then I'd suggest you extract the entire block of XML (using a regex or another appropriate method) and put it into an XML parser.