Skip to main content
Inspiring
December 25, 2016
Question

Any regExp expert here?

  • December 25, 2016
  • 2 replies
  • 889 views

How would you handle one or more hyphens in words? For instance, "T-cell" is a word,

<cfset w = "T-cell">,

REreplace("a long text string T-cell and more...", "#w#", "<a href=''>#w#</a>")

The above REreplace function failed to identify "T-cell" as one word since hyphen is used as an indicator for a range such as [a-z], according to Adobe documentation, thus, one needs to add the hyphen to the end to reference it as a literal, I attempted to do the following:

REreplace("a long text string T-cell and more...", "#w#-", "<a href=''>#w#</a>")  or

REreplace("a long text string T-cell and more...", "[#w#-]", "<a href=''>#w#</a>")

However, it misfired and caused serious problems. What's the correct regExp for this situation?

Many thanks

This topic has been closed for replies.

2 replies

BKBK
Community Expert
Community Expert
December 26, 2016

ghfftttyyudsderycv76 wrote:

How would you handle one or more hyphens in words? For instance, "T-cell" is a word

That implies that you wish to replace T-cell with T*cell, where the * stands for a character other than - or no character at all. But then it seems from the rest of the text that that is not the question you wish to ask.

REreplace("a long text string T-cell and more...", "#w#", "<a href=''>#w#</a>")

As cp_anil@rediff correctly says, this suggests that you wish to replace T-cell with <a href=''>T-cell</a>. If that is the case, then you could avoid regular expressions altogether and use the simpler,

replaceNoCase("a long text string T-cell and more...", w, "<a href=''>#w#</a>", "all")

Participating Frequently
December 26, 2016

Sorry if I have misunderstood your question, but if your intention is to change the word "T-cell" into "<a href=''>T-cell</a>" in the given example, the first option given by you itself will work fine.

There is nothing wrong in that at least what I could find. Because the hyphen would create an issue if it is inside a square bracket only.

So the piece of code

REreplace("a long text string T-cell and more...", "#w#", "<a href=''>#w#</a>")

will return

a long text string <a href=''>T-cell</a> and more... 

Please correct me if I have missed anything in your question.

Inspiring
December 26, 2016

Well, I've solved the problem.

But to appreciate your response and for the benefit of others...

I initially simplified the problem statement.  That is, I first read a simple HTML file into a var, then, ran

REreplace("#varName4longtext.", "#w#", "<a href=''>#w#</a>")

when var w contains "-" the above REreplace statement failed to identify w value in its entirety such as "T-cell", thus the question.

This is how to solve the problem after posting the question, I first replaced all of the occurrences of "-" into something else such as DASH and then ran REreplace (and probably replace would suffice as well...) and once done, I then revert the original "-" back.

BKBK
Community Expert
Community Expert
December 27, 2016

Thanks for sharing that. You were having troubl because the - is a special character in regex. You should therefore have escaped it with \.

Here is a suggestion:

<cfset w = "T-cell">

<cfset regex_w = "T\-cell">

...

<cfset newText = REreplaceNoCase(varName4longtext, regex_w, "<a href=''>#w#</a>", "all")>