Oh my god. It's full of code!

Posts tagged “string

Extracting Strings with RegEx in Salesforce Apex

Hey everyone. This is just another quick tip type of deal. It’s pretty easy to replace strings with Apex and regular expressions, but it’s a little bit harder to extract that string. In this example I want to extract a project number from an email subject line. The project number will be between { } braces. So how exactly do I got about doing this? Well it looks a bit like this.

string subject = 'this is a test {12312-D} email subject [dfasdfa]';
Pattern p = Pattern.compile('\\{([^}]*)\\}');
Matcher m = p.matcher(subject);
if (m.find()) 
{
   system.debug(m.group(1)); 
}

First I create a string to search. then I create matching pattern by using the pattern class and my nifty little regular expression. Remember to use Java style regular expressions! then I do a match against the subject line. After that I want to check and see if there was a match, and if so I debug out the first grouping. the 0th grouping contains the match with the brackets, and the 1st contains the text without. So there you have it. Just modify the regular expression to suit your needs and you should be on your way. Have fun!


Coldfusion number to text string function

Hey all,

I’ve been doing some work with phone systems that read text. One issue I hit is that they tend to want to read numbers as…. well numbers. For example an address like 2324 nowhwere street would come out sounding like ‘two thousand twenty-four nowhere street’. While correct, it’s not exactly what a caller wants to hear. So I threw together this quick function that will take a string and convert any numbers into their word equivalent. Hope this helps someone out there.

<cffunction name="numToText" hint="convert a number into text for the IVR system" returntype="string">
    <cfargument name="number" type="string" required="yes" hint="A string that contains numbers to convert">
    
    <cfset var returnString = "">
    
    <cfloop from="1" to="#len(arguments.number)#" index="i">
        <cfif isnumeric(mid(arguments.number,i,1))>
            <cfswitch  expression="#mid(arguments.number,i,1)#">
                <cfcase value="0">
                    <cfset appendString = "zero">
                </cfcase>
         
                <cfcase value="1">
                    <cfset appendString = "one">
                </cfcase>
        
                <cfcase value="2">
                    <cfset appendString = "two">
                </cfcase>
                
                <cfcase value="3">
                    <cfset appendString = "three">
                </cfcase>                        
        
                <cfcase value="4">
                    <cfset appendString = "four">
                </cfcase>
         
                <cfcase value="5">
                    <cfset appendString = "five">
                </cfcase>
        
                <cfcase value="6">
                    <cfset appendString = "six">
                </cfcase>
                
                <cfcase value="7">
                    <cfset appendString = "seven">
                </cfcase>
         
                 <cfcase value="8">
                    <cfset appendString = "eight">
                </cfcase>
                
                <cfcase value="9">
                    <cfset appendString = "nine">
                </cfcase> 
            </cfswitch> 
                <cfset returnString = returnString & " " & appendString>
            <cfelse>
                 <cfset appendString = mid(arguments.number,i,1)>
                 <cfset returnString = returnString & appendString>
        </cfif>   
                      
    </cfloop>
    
    <cfreturn returnString>
</cffunction>