2

I am trying to extract a string from an XML using the Regex. I am able to extract the desired result when I run below code in anonymous code from the developer console.

String textString = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac xr xr2 xr3" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" xr:uid="{7A1ABE4F-F5DD-4A1D-92CC-A67F4321A498}"><dimension ref="A1:D1"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="B10" sqref="B10"/></sheetView></sheetViews><sheetFormatPr defaultRowHeight="15" x14ac:dyDescent="0.25"/><cols><col min="1" max="1" width="21.85546875" customWidth="1"/><col min="2" max="2" width="33.5703125" customWidth="1"/><col min="3" max="3" width="34.42578125" customWidth="1"/><col min="4" max="4" width="35.28515625" customWidth="1"/></cols><sheetData><row r="1" spans="1:4" ht="29.25" thickBot="1" x14ac:dyDescent="0.3"><c r="A1" s="1" t="s"><v>[table_start_opportunities] {!Name}</v></c><c r="B1" s="2" t="s"><v>{!Amount}</v></c><c r="C1" s="2" t="s"><v>{!StageName}</v></c><c r="D1" s="3" t="s"><v>{!Probability} [table_end_opportunities]</v></c></row></sheetData><pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/><pageSetup paperSize="9" orientation="portrait" r:id="rId1"/></worksheet>';

String tablePattern = '\\[table_start.*?\\]\\s*((.[^\\[])+)\\s*\\[table_end.*?\\]';



List<String> tableMatchList = Utility.patterMatch(tablePattern,textString);
System.debug(tableMatchList);

public static List<String> patterMatch(String patternValue,String text){
        Pattern patternObj = Pattern.compile(patternValue);
        Matcher matcher = patternObj.matcher(text);
        System.debug(text);
        System.debug(patternValue);
        List<String> matches = new List<String>();
        while (matcher.find()) {
            String groupValue = matcher.group();
            System.debug(groupValue);
            matches.add(groupValue);
        }
        return matches;
    }

This text string variable is dynamic variable in class and I am using static text String copied from debug logs.

Is there anything I am missing which can affect the result in Apex class execution like Unicode or something like that?

Thanks

1
  • Have you added any debug logs to see what your "textString" looks like when ran in the code vs. anonymous apex? Nov 19, 2019 at 12:03

0

You must log in to answer this question.

Browse other questions tagged .