Regular Expressions in ColdFusion

How to trim leading zeros and trailing zeros using overriding?

This has something to do with the carriage and star and the dollar sign.

And 0.

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

+3
source share
6 answers
reReplace(string, "^0*(.*?)0*$", "$1", "ALL")

I.e:

^ = 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
+18
source
<cfset newValue = REReplace(value, "^0+|0+$", "", "ALL")>
+4
source

, - , ^ 0 + 0 + $ , :

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

, , .. .

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

, !

ColdFusion \ $, . \1 $1.

, :

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

:

^  = 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

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

, , - . , , , , , , . , , / .

   /**
    * 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;
    }

, - :

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

, - :)

0

Source: https://habr.com/ru/post/1785001/


All Articles