Regular Expression Examples

From CoolSolutionsWiki


This page is intended to be a resource for writing regular expressions in the context of Identity Manager. If you add more examples, please also include an example rule so the expression can be seen in context. A simulator trace may also be handy.


Contents

Format a Phone Number

This cleans a phone number and places it in the format of +1 (XXX) XXX-XXXX

<rule>
  <description>Format Telephone</description>
    <conditions>
      <and>
        <if-op-attr name="PHONE" op="available"/>
      </and>
    </conditions>
    <actions>
      <do-reformat-op-attr name="PHONE">
        <arg-value>
          <token-replace-all regex="\(" replace-with="">
            <token-replace-all regex="-" replace-with="">
              <token-replace-all regex="\)" replace-with="">
                <token-replace-all regex=" " replace-with="">
                  <token-op-attr name="PHONE"/>
                </token-replace-all>
              </token-replace-all>
            </token-replace-all>
          </token-replace-all>
        </arg-value>
      </do-reformat-op-attr>
      <do-if>
        <arg-conditions>
          <and>
            <if-op-attr mode="regex" name="PHONE" op="equal">..........</if-op-attr>
          </and>
        </arg-conditions>
        <arg-actions>
          <do-reformat-op-attr name="PHONE">
            <arg-value>
              <token-text xml:space="preserve">+1 (</token-text>
                <token-substring length="3">
                  <token-op-attr name="PHONE"/>
                </token-substring>
              <token-text xml:space="preserve">) </token-text>
                <token-substring length="3" start="3">
                  <token-op-attr name="PHONE"/>
              </token-substring>
              <token-text xml:space="preserve">-</token-text>
              <token-substring length="4" start="6">
                <token-op-attr name="PHONE"/>
              </token-substring>
            </arg-value>
          </do-reformat-op-attr>
        </arg-actions>
        <arg-actions>
          <do-strip-op-attr name="PHONE"/>
        </arg-actions>
    </do-if>
  </actions>
</rule>

Checking for Multiple Dest DNs in Matching

This rule checks to see if your matching rules found multiple matches. Only a single match is allowed so this can be used to trap errors.

<rule>
  <description>Detect Multiple Matches</description>
  <conditions>
    <and>
      <if-dest-dn op="available"/>
    </and>
  </conditions>
  <actions>
    <do-set-local-variable name="vDestDNValue">
      <arg-string>
        <token-dest-dn/>
      </arg-string>
    </do-set-local-variable>
    <do-if>
      <arg-conditions>
        <and>
          <if-local-variable mode="regex" name="vDestDNValue" op="equal">\uFFFC|\uFFFD</if-local-variable>
        </and>
      </arg-conditions>
     <arg-actions>
       <do-trace-message level="1">
         <arg-string>
           <token-text xml:space="preserve">MULTIPLE MATCHES FOUND</token-text>
         </arg-string>
       </do-trace-message>
      </arg-actions>
    </do-if>
  </actions>
</rule>

Count a Specific Number of Characters

This matches two characters or spaces

<arg-conditions>
  <and>
    <if-local-variable mode="regex" name="vCOB" op="equal">..</if-local-variable>
  </and>
</arg-conditions>

This matches 39 digits

<arg-conditions>
  <and>
    <if-local-variable mode="regex" name="vCOB" op="equal">\d{39}</if-local-variable>
  </and>
</arg-conditions>

This matches 7 to 11 non-digits

<arg-conditions>
  <and>
    <if-local-variable mode="regex" name="vCOB" op="equal">\D{7,11}</if-local-variable>
  </and>
</arg-conditions>

This matches 20 or more characters <arg-conditions>

  <and>
    <if-local-variable mode="regex" name="vCOB" op="equal">.{20,}</if-local-variable>
  </and>
</arg-conditions>

Look for a Starting Pattern

This checks for values of 'MYATTR' that begin with 10001 and can be of any longer length.

<arg-conditions>
  <and>
    <if-op-attr mode="regex" name="MYATTR" op="equal">10001.*</if-op-attr>
  </and>
</arg-conditions>

Replacing Words

This looks for the words Of and And and replaces them with all lower cased characters to result in of and and

<do-reformat-op-attr name="oaCurrentValue">
  <arg-value>
    <token-replace-all regex="\sOf\s" replace-with=" of ">
      <token-replace-all regex="\sAnd\s" replace-with=" and ">
        <token-op-attr name="oaCurrentValue"/>
      </token-replace-all>
    </token-replace-all>
  </arg-value>
</do-reformat-op-attr>

Case conversion

Convert aNY SpelLing to Title Case

<do-reformat-op-attr name="UPPERCASE">
  <arg-value>
    <token-replace-all regex="\ba" replace-with="A">
      <token-replace-all regex="\bb" replace-with="B">
        <token-replace-all regex="\bc" replace-with="C">
          [same for D,E,F...]
            <token-lower-case>
              <token-local-variable name="current-value"/>
            </token-lower-case>
          [another 23x...]
        </token-replace-all>
      </token-replace-all>
    </token-replace-all>
  </arg-value>
</do-reformat-op-attr>


Headline text