If you are unable to create a new account, please email support@bspsoftware.com

 

Need help with a predictable but variable text replacement expression please.

Started by FerdH4, Yesterday at 08:54:28 AM

Previous topic - Next topic

FerdH4

HAPPY NEW YEARS COGNOISe!

Using Cognos Analytics v11.2.3, I'm working with a text field named "Assigning Rule" which contains some repeating, useless characters as shown (between quotes) in actual example immediately below. 

"[ Rules Fired : General STAT (match), General STAT (select), General STAT (sort) ]"

I know how to use REPLACE to successfully carveout parts of the text like just the "(match)" or just the "(sort)". But that still leaves me with the text that lies between those two points.  I'm trying to craft an expression which will replace all the text starting from "(match)" through to "(sort)" with a single blank space.

In the original example above, the text to be blanked out is all of this:

" (match), General STAT (select), General STAT (sort) "

The end results after the expression is applied should look like this:

"[ Rules Fired : General STAT ]"

There are two complexities with this field in that the text between the "(match)" and "(sort)" points is both hugely variable text which is also of varying length.  The "(match)" and "(sort)" beginning and ending points are constants.

I'd appreciate any help with crafting a related replacement or substitution expression.

Thanks!

dougp

Here's a report spec that breaks down the pieces.  You'll still need to verify this against your values to be sure the end conditions are correct.  You'll also want to consider what happens if the value doesn't contain '(match)' or '(sort)' or both.

There may also be a slick way to accomplish this using regular expressions, but since support varies across database platforms, I thought it would be best to keep this simple.

This was created using Cognos Analytics 12.0.4FP1, so you will need to change the xmlns value to match what 11.2.3 produces.

<report xmlns="http://developer.cognos.com/schemas/report/17.2/" useStyleVersion="11.6" expressionLocale="en-us">
    <drillBehavior/>
    <layouts>
        <layout>
            <reportPages>
                <page name="Page1">
                    <style>
                        <defaultStyles>
                            <defaultStyle refStyle="pg"/>
                        </defaultStyles>
                    </style>
                    <pageBody>
                        <style>
                            <defaultStyles>
                                <defaultStyle refStyle="pb"/>
                            </defaultStyles>
                        </style>
                        <contents>
                            <list horizontalPagination="true" name="List1" refQuery="Query1">
                                <noDataHandler>
                                    <contents>
                                        <block>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <staticValue>No Data Available</staticValue>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                            <style>
                                                <CSS value="padding:16px;"/>
                                            </style>
                                        </block>
                                    </contents>
                                </noDataHandler>
                                <style>
                                    <CSS value="border-collapse:collapse"/>
                                    <defaultStyles>
                                        <defaultStyle refStyle="ls"/>
                                    </defaultStyles>
                                </style>
                                <listColumns>
                                    <listColumn>
                                        <listColumnTitle>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lt"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemLabel refDataItem="Assigning Rule"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnTitle>
                                        <listColumnBody>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lm"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemValue refDataItem="Assigning Rule"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnBody>
                                    </listColumn>
                                    <listColumn>
                                        <listColumnTitle>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lt"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemLabel refDataItem="loc match"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnTitle>
                                        <listColumnBody>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lm"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemValue refDataItem="loc match"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnBody>
                                    </listColumn>
                                    <listColumn>
                                        <listColumnTitle>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lt"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemLabel refDataItem="loc sort"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnTitle>
                                        <listColumnBody>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lm"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemValue refDataItem="loc sort"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnBody>
                                    </listColumn>
                                    <listColumn>
                                        <listColumnTitle>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lt"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemLabel refDataItem="before"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnTitle>
                                        <listColumnBody>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lm"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemValue refDataItem="before"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnBody>
                                    </listColumn>
                                    <listColumn>
                                        <listColumnTitle>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lt"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemLabel refDataItem="after"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnTitle>
                                        <listColumnBody>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lm"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemValue refDataItem="after"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnBody>
                                    </listColumn>
                                    <listColumn>
                                        <listColumnTitle>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lt"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemLabel refDataItem="output"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnTitle>
                                        <listColumnBody>
                                            <style>
                                                <defaultStyles>
                                                    <defaultStyle refStyle="lm"/>
                                                </defaultStyles>
                                            </style>
                                            <contents>
                                                <textItem>
                                                    <dataSource>
                                                        <dataItemValue refDataItem="output"/>
                                                    </dataSource>
                                                </textItem>
                                            </contents>
                                        </listColumnBody>
                                    </listColumn>
                                </listColumns>
                            </list>
                        </contents>
                    </pageBody>
                    <XMLAttributes>
                        <XMLAttribute output="no" name="RS_legacyDrillDown" value="0"/>
                    </XMLAttributes>
                </page>
            </reportPages>
        </layout>
    </layouts>
    <XMLAttributes>
        <XMLAttribute output="no" name="RS_CreateExtendedDataItems" value="true"/>
        <XMLAttribute output="no" name="listSeparator" value=","/>
        <XMLAttribute output="no" name="decimalSeparator" value="."/>
        <XMLAttribute output="no" name="RS_modelModificationTime" value="2015-11-25T21:38:24.820Z"/>
    </XMLAttributes>
    <queries>
        <query name="Query1">
            <source>
                <model/>
            </source>
            <selection>
                <dataItem name="Assigning Rule">
                    <expression>&apos;[ Rules Fired : General STAT (match), General STAT (select), General STAT (sort) ]&apos;</expression>
                    <XMLAttributes>
                        <XMLAttribute output="no" name="RS_dataType" value="3"/>
                        <XMLAttribute output="no" name="RS_dataUsage" value=""/>
                    </XMLAttributes>
                </dataItem>
                <dataItem name="loc match">
                    <expression>position (&apos;(match)&apos;, [Assigning Rule])</expression>
                    <XMLAttributes>
                        <XMLAttribute output="no" name="RS_dataType" value="1"/>
                        <XMLAttribute output="no" name="RS_dataUsage" value=""/>
                    </XMLAttributes>
                </dataItem>
                <dataItem name="loc sort">
                    <expression>position(&apos;(sort)&apos;, [Assigning Rule])</expression>
                    <XMLAttributes>
                        <XMLAttribute output="no" name="RS_dataType" value="1"/>
                        <XMLAttribute output="no" name="RS_dataUsage" value=""/>
                    </XMLAttributes>
                </dataItem>
                <dataItem name="before">
                    <expression>substring ([Assigning Rule], 1, [loc match] - 1)</expression>
                    <XMLAttributes>
                        <XMLAttribute output="no" name="RS_dataType" value="3"/>
                        <XMLAttribute output="no" name="RS_dataUsage" value="0"/>
                    </XMLAttributes>
                </dataItem>
                <dataItem name="after">
                    <expression>substring ([Assigning Rule], [loc sort] + 6)</expression>
                    <XMLAttributes>
                        <XMLAttribute output="no" name="RS_dataType" value="3"/>
                        <XMLAttribute output="no" name="RS_dataUsage" value="0"/>
                    </XMLAttributes>
                </dataItem>
                <dataItem name="output">
                    <expression>[before] || [after]</expression>
                    <XMLAttributes>
                        <XMLAttribute output="no" name="RS_dataType" value="3"/>
                        <XMLAttribute output="no" name="RS_dataUsage" value="0"/>
                    </XMLAttributes>
                </dataItem>
            </selection>
        </query>
    </queries>
    <modelPath>/content/folder[@name=&apos;Samples&apos;]/folder[@name=&apos;Models&apos;]/package[@name=&apos;GO sales (query)&apos;]/model[@name=&apos;model&apos;]</modelPath>
</report>

FerdH4

WHOA!  Actual XML code!  Thanks.

I'll test this very soon.  I'll post another Comment if there are any complications.  So please accept this "Thanks" dougp now in case I don't make it back for awhile.

FerdH4

The XML worked beautifully right out of the box. 

Unfortunately, while testing with a larger dataset I found that the Rules can rarely be plural - I guess that's why they are "Rules" with an "s" on the end.  Here is the one example I've found so far:

[ Rules Fired : Daily credit @west (match), Daily credit @west (select), Daily redit@clay (match), Daily credit@clay (select), Daily credit@clay (sort) ]

It appears to have two occurrences of "(match)" and still only a single "(sort)".  My preference would be to just not try to parse those specific Rules.

Any idea for how I might easily adjust your five-part expression to just fully exclude like records from being processed through the rest of the expression?  Can you think of an IF condition what would easily identify the occurs of TWO "(match)" statements within a single field?




dougp