Showing posts with label String Matching. Show all posts
Showing posts with label String Matching. Show all posts

Thursday, December 13, 2012

Add Space Before Capital Letter

In one of my recent project my requirement was to add a space before a capital letter like, if our word is "HiAmbika" then we have to convert it to "Hi Ambika".

Example: 
Input String: "RajaRamMohamRoy"
Output       :  "Raja Ram Mohan Roy"

I just used regular expression in ColdFusion to achieve this as follows.


We can also do other advance string matching functionality with back referencing. We will see that latter.


Wednesday, December 12, 2012

Extract Text from HTML Code in ColdFusion

Our Goal:
How we could extract text from HTML code using ColdFusion?

Description:
We will use some regular expression to achieve this.

 Use regular expression "<.*?>" to extract HTML text. This one will work fine until there is no JavaScript(JS) or CSS code is present inside the HTML. When any JS/CSS code is present in HTML then it can't omit those.Then use a second regular expression to remove JS and CSS code from HTML.

To replace JS and CSS code we have to use "<(script|style).*?</\1>".

So, if we will combine the two regular expression then we can get actual text from the HTML code which may contain some CSS and JS code.

The final regular expression will be "<(script|style).*?</\1>|<.*?>".

Example:
Our HTML code is:


So, the final ColdFusion code to extract text from above HTML would be follows:


After, all these steps we will get following text as the out put.

NOTE:
In the final regular expression "<(script|style).*?</\1>|<.*?>", we have used expression to remove any CSS/JS first then remove the HTML. As if we will change the order to "<.*?>|<(script|style).*?</\1>" then the CSS/JS code will be there in the final output. As the CSS/JS code will match with the first part and it will treat as normal HTML code.

Tuesday, February 07, 2012

Case In-Sensitive String matching in Query Of Query(QoQ ColdFusion - 9)

We know that for string matching in database we use "LIKE" Operator. We can also do the string matching operation on a query object by Query Of Query(QoQ) in ColdFusion like the following way.


/*In this example I am calling a stored procedure myProc and passing the argument as ntMLSID then it returns all the field details . In the next query I am filtering the result by matching the string to a returned field "vcFieldName"*/

<cfstoredproc datasource="#application.dsn#" procedure="myProc">
  <cfprocresult name="fieldDetails" />
  <cfprocparam type="in" cfsqltype="CF_SQL_INTEGER" value="#variables.ntMLSID#" />
</cfstoredproc>

<cfquery dbtype="query" name="filterResult">
    SELECT *
    FROM
         fieldDetails
    WHERE
        vcFieldName LIKE '%#variables.vcSearchedField#%'
</cfquery>

Here in the above query everything is seems good. But the issue is that the above set of code only can able to match(comparison) a case sensitive string .

e.g- If vcFieldName in the Query is "India" and we pass the search string as "ind" then it will not return any value. By default the string matching by the LIKE operator of QoQ is case sensitive. 

So, how we will make it as case insensitive ???

We can do that by following ways:


<cfquery dbtype="query" name="filterResult">
    SELECT *
    FROM
         fieldDetails
    WHERE
        LOWER(vcFieldName) LIKE '%#LCASE(variables.vcSearchedField)#%'
</cfquery>

In first case we use LOWER and LCASE functions . Similarly we can also use UPPER and UCASE to  do  the same as follow:



<cfquery dbtype="query" name="filterResult">
    SELECT *
    FROM
         fieldDetails
    WHERE
        UPPER(vcFieldName) LIKE '%#UCASE(variables.vcSearchedField)#%'
</cfquery>

Followers