Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

RegEx Help

Explorer ,
Sep 06, 2011 Sep 06, 2011

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

979
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 06, 2011 Sep 06, 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

Translate
Guide ,
Sep 06, 2011 Sep 06, 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 06, 2011 Sep 06, 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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 06, 2011 Sep 06, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 06, 2011 Sep 06, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 06, 2011 Sep 06, 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,"$")>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 06, 2011 Sep 06, 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Sep 06, 2011 Sep 06, 2011

@Adam

Thanks for the updated code, that seems to have done the trick!

Do you use any resources or anything to build these ugly expressions? Books, websites, etc.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 06, 2011 Sep 06, 2011
LATEST

Do you use any resources or anything to build these ugly expressions? Books, websites, etc.

Just the CF docs, really:

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec0a38f-7ffb.html

And a lot of practise & experimentation.  Years ago, I made a point of trying to use them whenever I thought there might be good grounds to, so forced myself to practise.  After a while it becomes easier to read & write them.

I'm still learning though.  I need to sit down and really think about how look-aheads and stuff work, when I need to use one.

I use Regex Coach for testing them:

http://weitz.de/regex-coach/

It's not 100% compatible with the regex library that CF uses, but it's close enough for most purposes.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources