Regular Expression Functions

Overview

This Plug-in exposes Java Regular Expression string manipulation capabilities as Appian Functions. Very useful for validation rules and data manipulation on interactive SAIL forms.  

Key Features & Functionality

Datatypes provided:

  • RegexMatch - has a string field representing the string found, a starting index for the string in the haystack and an ending index for the string in the haystack

Regex Flags supported:

  • i - case insensitive
  • g - find all matches
  • m - multiline search
  • s - treats the entire string as a single line
  • u - unicode aware search
  • x - ignores comments starting with '#' and white spaces
  • d - enables unix line mode

Functions provided:

  • regexSearch - Searches for the selected pattern with the specified regex options and returns a list of RegexMatch Datatypes, accepts all regex flags.
  • regexArraySearchIndexOfFirstMatch - Searches an array for a match and returns the first index of match found
  • regexMatch - Indicates whether the regular expression finds a match in the input string, accepts all regex flags.
  • regexInsertMatchMarkers - Finds the match or matches and surrounds them with starting and ending markers, accepts all regex flags.
  • regexFirstMatch - Returns the first match of the regular expression within the input string, accepts all regex flags except 'g'.
  • regexAllMatches - Returns all matches of the regular expression within the input string, accepts all regex flags.
  • regexReplaceAll - Replaces all matches of the regular expression within the input string
Anonymous
Parents
  • Chris,

    I really really need the group captures from regexSearch().  My client doesn't seem to have an agreement to develop & install plugins to their cloud instances.

    I was wondering if you could make the necessary changes to your plugin code and get it into marketplace.  I fashioned it with what you had already implemented.

    First, I created RegexHelperFunctionsGroup.java.  It's based off of your RegexHelperFunctions.java.  I added the ability to append the captured groups text using "||GRP=>" so the result of a regex search can be easily split() into the matched string, then followed by any captured groups.

    What this will mean, you'll need to copy RegexSearch.java to RegexSearchGroup.java and have that call RegexHelperFunctionsGroup.java's Search() instead of RegexHelperFunctions.java's Search() method.

    Is this something that can be done asap?  Thanks.

    RegexHelperFunctionsGroup.java

    package com.appiancorp.plugins.regexfunctions.functions;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    import com.appiancorp.plugins.regexfunctions.pojos.RegexMatch;


    public class RegexHelperFunctionsGroup {

      public RegexMatch[] Search(String pattern, String searchString, String regexFlags) {
        int flags = 0;
        boolean returnAfterFirst = true;

        for (int i = 0; i < regexFlags.length(); i++) {
          switch (regexFlags.charAt(i)) {
          case 'i':
            flags = flags | Pattern.CASE_INSENSITIVE;
            break;
          case 'm':
            flags = flags | Pattern.MULTILINE;
            break;
         case 's':
            flags = flags | Pattern.DOTALL;
            break;
         case 'u':
            flags = flags | Pattern.UNICODE_CASE;
            break;
         case 'x':
            flags = flags | Pattern.COMMENTS;
            break;
         case 'd':
            flags = flags | Pattern.UNIX_LINES;
            break;
         case 'g':
            returnAfterFirst = false;
            break;
         }
       }

       Pattern p = Pattern.compile(pattern, flags);
       Matcher m = p.matcher(searchString);
       StringBuffer captures = new StringBuffer();
       List<RegexMatchGroup> foundList = new ArrayList<RegexMatchGroup>();
        while (m.find()) {

          captures.delete(0, captures.length());
          for (int i = 1; i <= m.groupCount(); i++) {
            captures.append("||GRP=>" + m.group(i));
          }

          foundList.add(new RegexMatchGroup(m.group() + ((captures.length() > 0) ? captures : null), m.start(), m.end()));

          /* If we only wanted the first instance, return after finding it */
          if (returnAfterFirst)
            break;
        }

        return foundList.toArray(new RegexMatchGroup[foundList.size()]);
      }

    }

Comment
  • Chris,

    I really really need the group captures from regexSearch().  My client doesn't seem to have an agreement to develop & install plugins to their cloud instances.

    I was wondering if you could make the necessary changes to your plugin code and get it into marketplace.  I fashioned it with what you had already implemented.

    First, I created RegexHelperFunctionsGroup.java.  It's based off of your RegexHelperFunctions.java.  I added the ability to append the captured groups text using "||GRP=>" so the result of a regex search can be easily split() into the matched string, then followed by any captured groups.

    What this will mean, you'll need to copy RegexSearch.java to RegexSearchGroup.java and have that call RegexHelperFunctionsGroup.java's Search() instead of RegexHelperFunctions.java's Search() method.

    Is this something that can be done asap?  Thanks.

    RegexHelperFunctionsGroup.java

    package com.appiancorp.plugins.regexfunctions.functions;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    import com.appiancorp.plugins.regexfunctions.pojos.RegexMatch;


    public class RegexHelperFunctionsGroup {

      public RegexMatch[] Search(String pattern, String searchString, String regexFlags) {
        int flags = 0;
        boolean returnAfterFirst = true;

        for (int i = 0; i < regexFlags.length(); i++) {
          switch (regexFlags.charAt(i)) {
          case 'i':
            flags = flags | Pattern.CASE_INSENSITIVE;
            break;
          case 'm':
            flags = flags | Pattern.MULTILINE;
            break;
         case 's':
            flags = flags | Pattern.DOTALL;
            break;
         case 'u':
            flags = flags | Pattern.UNICODE_CASE;
            break;
         case 'x':
            flags = flags | Pattern.COMMENTS;
            break;
         case 'd':
            flags = flags | Pattern.UNIX_LINES;
            break;
         case 'g':
            returnAfterFirst = false;
            break;
         }
       }

       Pattern p = Pattern.compile(pattern, flags);
       Matcher m = p.matcher(searchString);
       StringBuffer captures = new StringBuffer();
       List<RegexMatchGroup> foundList = new ArrayList<RegexMatchGroup>();
        while (m.find()) {

          captures.delete(0, captures.length());
          for (int i = 1; i <= m.groupCount(); i++) {
            captures.append("||GRP=>" + m.group(i));
          }

          foundList.add(new RegexMatchGroup(m.group() + ((captures.length() > 0) ? captures : null), m.start(), m.end()));

          /* If we only wanted the first instance, return after finding it */
          if (returnAfterFirst)
            break;
        }

        return foundList.toArray(new RegexMatchGroup[foundList.size()]);
      }

    }

Children