Phillip Senn
Phillip Senn

Reputation: 47635

Regular Expressions in ColdFusion

How do I trim leading zeros and trailing zeros using rereplace?

It has something to do with a caret and a star and a dollar sign.

And a 0.

Here's a cheat sheet: http://www.petefreitag.com/cheatsheets/regex/

Upvotes: 3

Views: 3391

Answers (6)

surgiie
surgiie

Reputation: 4295

This post is quite old but I'm posting in case anyone finds it useful. I have found myself needing to trim custom characters on multiple occasions so I want to share a recent helper I wrote to trim any custom character using rereplace if you find it useful. It works just like regular trim but you can pass any custom character string as the 2nd param and it will trim all leading/trailing characters.

   /**
    * Trims leading and trailing characters using rereplace
    * @param string - string to trim
    * @param string- custom character to trim
    * @return string - result
    */
    function $trim(required string, string customChar=" "){
        var result = arguments.string;

        var char = len(arguments.customChar) ? left(arguments.customChar, 1) : ' ';

        char = reEscape(char);

        result = REReplace(result, "#char#+$", "", "ALL");
        result = REReplace(result, "^#char#+", "", "ALL");

        return result;
    }

In your case you can just use this helper to do something like:

string = "0000foobar0000";
string = $trim(string, "0");
//string now "foobar"

hope this helps someone :)

Upvotes: 0

Raffael Meier
Raffael Meier

Reputation: 309

The above does not work at all, except bradley's answer!

In ColdFusion, to reference a capture group, you need \ instead of $, e.g. \1 instead of $1.

So the correct answer is:

reReplace(string, "^0*(.*?)0*$", "\1", "ALL")

That is:

^  = starting with
0* = the character "0", zero or more times
() = capture group, referenced later as $1
.* = any character, zero or more times
*? = zero or more, but lazy matching; try not to match the next character
0* = the character "0", zero or more times, this time at the end
$  = end of the string

And:

\1 reference to capture group 1  (see above, introduced by ( )

Upvotes: 1

Bradley Moore
Bradley Moore

Reputation: 748

This seems to work.. would check for your use cases.

<cfset sTest= "0001" />
<cfset sTest= "leading zeros? 0001" />
<cfset sTest= "leading zeros? 0001.02" />
<cfset sTest= "leading zeros? 0001." />
<cfset sTest= "leading zeros? 0001.2" />

<cfset sResult= reReplace( sTest , "0+([0-9]+(\.[0-9]+)?)" , "\1" , "all" ) />

Upvotes: 1

Nathan Strutz
Nathan Strutz

Reputation: 8123

reReplace(string, "^0*(.*?)0*$", "$1", "ALL")

That is:

^ = starting with
0* = the character "0", zero or more times
() = capture group, referenced later as $1
.* = any character, zero or more times
*? = zero or more, but lazy matching; try not to match the next character
0* = the character "0", zero or more times, this time at the end
$ = end of the string

Upvotes: 19

morja
morja

Reputation: 8560

I am not a coldfusion expert, but something like, replace all ^0+ and 0+$ by empty string, e.g.:

REReplace("000xyz000","^0+|0+$","")

Upvotes: 1

Tomalak
Tomalak

Reputation: 338336

<cfset newValue = REReplace(value, "^0+|0+$", "", "ALL")>

Upvotes: 4

Related Questions