Skip to main content
Inspiring
September 6, 2011
Answered

RegEx Help

  • September 6, 2011
  • 2 replies
  • 1252 views

I need a little of RegEx help based on the following:

When there is a price associated with an element and an underscore is present.  The text that appears from the underscore to the price should be suppressed. For example:

The following string:

Cash Back_Requires Redemption _$5 for 12 Months Cl  $0.00


Would be translated to:
Cash Back $0.00

TIA

    This topic has been closed for replies.
    Correct answer Adam Cameron.

    Your RegEx seems to remove all characters.

    [...]

    <cfset myString = REReplaceNoCase(myString,"^([^_]*?)_.*?(\$[^$]+?)$","","all")>

    Well it would do... you're replacing the match with nothing!

    You need to replace it with the two captured subexpressions, eg:

    s = reReplace(s, "^([^_]*?)_.*?(\$[^$]+?)$", "\1 \2", "ONE");

    Sorry, I thought (well... I dunno how much I was thinking, to be honest ;-) you just needed help with the regex, not with how to use it.  Hence just posting the regex.

    --

    Adam

    2 replies

    Inspiring
    September 6, 2011

    When there is a price associated with an element and an underscore is present.  The text that appears from the underscore to the price should be suppressed. For example:

    The following string:

    Cash Back_Requires Redemption _$5 for 12 Months Cl  $0.00


    Would be translated to:
    Cash Back $0.00

    If I understand your requirement correctly, this should do it:

    ^([^_]*?)_.*?(\$[^$]+?)$

    I have to concede I didn't test all edge cases, but it seems to work.

    --

    Adam

    BalanceAuthor
    Inspiring
    September 6, 2011

    @Adam,

    Your RegEx seems to remove all characters.

    Here's my CF code that verifies this:

    <cfsavecontent variable="myString">

    Cash Back_Requires Redemption _$5 for 12 Months Cl  $0.00

    </cfsavecontent>

    <cfset myString = REReplaceNoCase(myString,"^([^_]*?)_.*?(\$[^$]+?)$","","all")>

    <p>len(myString) = <cfoutput>#len(myString)#</cfoutput></p> <!--- this returns 1, which is just an empty space --->

    Here's my version, which so far, seems to work:

    <cfset myString = listDeleteAt(myString,2,"_")>

    <cfset myString = replace(myString,"_"," ","all")>

    <cfset myString = listDeleteAt(myString,2,"$")>

    Adam Cameron.Correct answer
    Inspiring
    September 6, 2011

    Your RegEx seems to remove all characters.

    [...]

    <cfset myString = REReplaceNoCase(myString,"^([^_]*?)_.*?(\$[^$]+?)$","","all")>

    Well it would do... you're replacing the match with nothing!

    You need to replace it with the two captured subexpressions, eg:

    s = reReplace(s, "^([^_]*?)_.*?(\$[^$]+?)$", "\1 \2", "ONE");

    Sorry, I thought (well... I dunno how much I was thinking, to be honest ;-) you just needed help with the regex, not with how to use it.  Hence just posting the regex.

    --

    Adam

    Owainnorth
    Inspiring
    September 6, 2011

    Don't have my regex tester to hand, but you could always just use list functions:

    <cfset str = "Cash Back_Requires Redemption _$5 for 12 Months Cl  $0.00" />

    <cfoutput>#listFirst(str, '_')# #listLast(str, '$')#</cfoutput>

    BalanceAuthor
    Inspiring
    September 6, 2011

    @Owain - Your suggestion kind of works, but the dollar sign is removed from 0.00 (and it shouldn't). Here's my version which, so far, seems to work:

    <cfsavecontent variable="myString">

    Cash Back_Requires Redemption _$5 for 12 Months Cl  $0.00

    </cfsavecontent>

    <cfset myString = listDeleteAt(myString,2,"_")>

    <cfset myString = replace(myString,"_"," ","all")>

    <cfset myString = listDeleteAt(myString,2,"$")>

    <p><cfoutput>#myString#</cfoutput></p>

    Owainnorth
    Inspiring
    September 6, 2011

    @Owain - Your suggestion kind of works, but the dollar sign is removed from 0.00 (and it shouldn't).

    You could always just put the dollar sign back in?

    <cfsavecontent variable="myString">

    Cash Back_Requires Redemption _$5 for 12 Months Cl  $0.00

    </cfsavecontent>

    Be careful using cfsavecontent for creating a simple string, it will've included a newline at the beginning and end of the string, which may or may not cause you issues in different circumstances. I'd use a simple cfset.