开发者

Regular Expressions in ColdFusion

开发者 https://www.devze.com 2023-02-04 01:42 出处:网络
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.

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/


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


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


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

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


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" ) />


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 ( )


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 :)

0

精彩评论

暂无评论...
验证码 换一张
取 消