Link to home
Start Free TrialLog in
Avatar of roger v
roger vFlag for United States of America

asked on

Coldfusion regular expression not working as intended.!

Hi,

I'm having a really crazy issue - the coldfusion replacenocase function does not work! Neither does the ReReplace function. All I'm trying to do is replace a single quote " ' ", with a space. But the problem is that the single quote is inside a pair of parentheses, so coldfusion does not capture the single quote! How do I resolve this?
<!---the string that has the single quote inside parenthesis--->
<cfset badVal = "Inchon (Inch'on) (Incheon)">

<!---the regexp I'm using to get rid of the single quote, but won't work--->
<cfset goodVal = trim(ReReplace(badVal,"'","","all")>

<!---the dump shows that goodval still has the single quote with the parenthesis around it! --->
<cfdump var="#goodVal#">

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of roger v

ASKER

@kaufmed,

Good question. I'm not sure, that's how it's stored in the MSSQL db. Even if it is, could I look for single quote OR any funny character and strip it out?
@sjklein42

It didn't for my test   :  )


@roger_v

I tested your code and it seems to work for me. As for searching for single and "funny" chars, you can simply add them to your search criteria. For example:

<cfset goodVal = trim(ReReplace(badVal,"['´']","","all"))>

Open in new window

<cfset badVal = "Inchon (Inch'on) (Incheon)">

<cfset goodVal = trim(ReReplace(badVal,"'","","all"))>

<cfdump var="#badVal#">
<br />
<cfdump var="#goodVal#">

Open in new window

untitled.PNG
And if that doesn't work, then try this:

<cfset goodVal = trim(REReplace(badVal,"\'","","all")>

Open in new window

I didn't do a very good job of organizing...    : (

The top code section is for the last comment. The bottom code section and screenshot apply to me testing your existing code.
Single quote isn't a special character in regex, sjklein42; it does not need to be escaped.
Avatar of roger v

ASKER

@kaufmed & @sjklein,

I tried both of y'all's approaches - didn't work. Every other single quote is stripped out except for the one that is in between the parentheses - ( )
I was just giving an example. It's most likely a straight apostrophe (I don't know the proper name). You'll probably have to search the unicode tables to find it. I've expanded my previous list below, but I don't know if I managed to capture it. Scan the unicode tables for anything that looks like an apostrophe and add it to the list below.
<cfset goodVal = trim(ReReplace(badVal,"['´'’¿‘''¿`´]","","all"))>

Open in new window

The question marks are not part of the input. EE (or the browser) didn't like those characters, so those are placeholders. The intent of the above post remains the same, however.
Better yet...   Just copy the character from a dump of that column...  then paste it into the patter   :  )
Avatar of roger v

ASKER

Yup that solved it. Thanks!
NP. Glad to help  : )