Coldfusion substring via regex

Discussion in 'Programming' started by khu84, Oct 1, 2009.

  1. #1
    In coldfusion is there a function which takes a regular expression and a string and return a sub-string...?
     
    khu84, Oct 1, 2009 IP
  2. cfStarlight

    cfStarlight Peon

    Messages:
    398
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    cfStarlight, Oct 1, 2009 IP
  3. JasonBartholme

    JasonBartholme Peon

    Messages:
    396
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This is a working example of what you are looking for. I suggest if you do regex matches, wrap the code around a catch/try if there is no match.

    <cfscript>
    content = 'The quick brown fox jumped over the lazy dogs.';
    regExp = '(fox)';

    //Trim the string just in case there are spaces to throw off the position count
    trimDoc = trim(content);

    //This if there is a match, it returns an array with position(pos) and length(length)
    result = REfindNoCase(regExp,trimDoc,1,"Yes");

    //Save the match to a query based on the position and length of the match
    matchedString = mid(trimDoc,result.pos[2],result.len[2]);
    </cfscript>

    <cfoutput>
    #matchedString#
    </cfoutput>
     
    JasonBartholme, Oct 8, 2009 IP