From WikiChip
Difference between revisions of "mirc/identifiers/$replace"
< mirc‎ | identifiers

(Synopsis)
(See also)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{mirc title|$replace Identifier}}'''$replace''' Performs search/replace on text strings.
+
{{mirc title|$replace Identifier}}'''$replace''' Performs search/replace on text strings. '''$replacecs''' is the case sensitive version.
 +
 
 +
$replace can replace an already replaced string, it searches all substrings at every position in the text and start from the beginning of the string whenever there is a replacement made, {{mIRC|$replacex}} can be used to prevent what has already been replaced from being checked again
 +
 
  
 
== Synopsis ==
 
== Synopsis ==
 +
<pre>
 +
$replace(text, substring, replace, stringN, replaceN)
 +
$replacecs(text , substring, replace, stringN, replaceN)
 +
</pre>
  
$replace( String, string, replace, stringN, replaceN)
+
== Parameters ==
 +
* '''text''' - The string you want to replace from
 +
* '''substring''' - The 1st string you want to search for
 +
* '''replace''' - the replacement for the 1st string
 +
* '''substringN''' - The Nth string you want to search for
 +
* '''replaceN''' - the replacement for the Nth string
  
== Parameters ==
 
*'''string''' Text string that's the target of the search/replace
 
*'''SearchString''' 1st Search String
 
*'''ReplaceString''' Replacement string for all matches of SearchString1.
 
  
*Note: Excluding the target String, there must be an even number of search/replace parameters, even if any of them are $null. $replace(test,1) is an error but $replace(test,1,) is not. The number of search/replace pairs is limited only by the 4150 limit for the entire command.
+
'''Note''': Excluding the first parameter (input string), there must be an even number of substring/replace parameters
  
*Note: The search is case-insensitive unless you use the case-sensitive equivalent $replacecs. $replacex also does search/replace, but searches in a different way.
 
 
== Properties ==
 
== Properties ==
 
 
None
 
None
 
== Notes ==
 
 
If your search term contains a comma or parenthesis you must either use $chr(44) $chr(40) $chr(41) or place the term within a %variable.
 
 
Depending on which kind of search/replace you need, you can use either $replace or $replacex, their case-sensitive equivalents $replacecs or $replacexcs, or if you need regular expressions use $regsub or $regsubex.
 
 
$replace does a search/replace of a string using the first pair of search/replace strings, possibly doing multiple replacements. As it makes each replacement, the pointer is moved to the character following that replacement before continuing the search for another replacement using that same search term, until it reaches the end of the string. As long as there are additional pairs of search/replace strings, it uses the string modified by the prior search/replace pair as the target for search/replace using that next pair of parameters.
 
 
In effect, these are the same thing:
 
 
<source lang="mIRC">$replace( string,search1,replace1,search2,replace2 )
 
$replace( $replace(string,search1,replace1) ,search2,replace2)</source>
 
 
$replacex does the search/replace differently. It begins with the 1st character of the original string, then tries each search/replace pair in left-to-right order until it finds a match. If no search/replace pair affects the 1st character, it moves the pointer to the next character. If there is a search/replace which affects the 1st character, it makes the replacement then sets the pointer at the character following that replacement. (The earliest character not yet involved in a search/replace) It then repeats the same search/replace rules at the new pointer's position, then continues repeating until it reaches the end of the string.
 
  
 
== Examples ==
 
== Examples ==
Line 106: Line 97:
 
//var %sp $chr(32) | tokenize 32 $me $+ !?,*() | var %string $replace($1-,$chr(44),%sp,.,%sp,',%sp,',%sp,?,%sp,*,%sp,$chr(40),%sp,$chr(41),%sp ,!,%sp,;,%sp,:,%sp) | echo -a $1- / $istok(%string,$me,32)
 
//var %sp $chr(32) | tokenize 32 $me $+ !?,*() | var %string $replace($1-,$chr(44),%sp,.,%sp,',%sp,',%sp,?,%sp,*,%sp,$chr(40),%sp,$chr(41),%sp ,!,%sp,;,%sp,:,%sp) | echo -a $1- / $istok(%string,$me,32)
 
</source>
 
</source>
 +
  
 
== Compatibility ==
 
== Compatibility ==
Line 111: Line 103:
  
 
== See also ==
 
== See also ==
* {{mIRC|$replacecs}}
 
 
* {{mIRC|$replacex}}
 
* {{mIRC|$replacex}}
* {{mIRC|$replacexcs}}
 
 
* {{mIRC|$regsub}}
 
* {{mIRC|$regsub}}
 
* {{mIRC|$regsubex}}
 
* {{mIRC|$regsubex}}

Latest revision as of 10:48, 25 February 2018

$replace Performs search/replace on text strings. $replacecs is the case sensitive version.

$replace can replace an already replaced string, it searches all substrings at every position in the text and start from the beginning of the string whenever there is a replacement made, $replacex can be used to prevent what has already been replaced from being checked again


Synopsis[edit]

$replace(text, substring, replace, stringN, replaceN)
$replacecs(text , substring, replace, stringN, replaceN)

Parameters[edit]

  • text - The string you want to replace from
  • substring - The 1st string you want to search for
  • replace - the replacement for the 1st string
  • substringN - The Nth string you want to search for
  • replaceN - the replacement for the Nth string


Note: Excluding the first parameter (input string), there must be an even number of substring/replace parameters

Properties[edit]

None

Examples[edit]

//echo -a $replace(Abcdabcd, a,b, a,c )
The 2nd pair does nothing because the first pair replaced all the a's with b's. Search is case-insensitive so both 'A' and 'a' match against 'a'.
result: bbcdbbcd
 
//var %string $chr(233) , %search $upper(%string) | echo -a $replace( %string , %search ,x)
result: é
case-sensitive applies only to a-z A-Z ranges, and does not apply the same rules as $upper and $lower.
 
//echo -a $replace(abcdabcd, a,b ,b,c )
The 1st pair changes the string to bbcdbbcd
The 2nd pair changes the modified string, changing all the b's into c's
result: cccdcccd
 
//echo -a $replace(abcdabcd, a,aa, a,x)
The 1st pair changes each 'a' into 'aa', modifying string to aabcdaabcd
The 2nd pair changes the modified string, changing the 4 'a' strings into 'x' strings.
result: xxbcdxxbcd
 
//echo -a $replace(abcdabcd, a,x, a,aa)
With the search pairs in the opposite order, the first pair changes string to xbcdxbcd
The 2nd pair does nothing because there are no 'a' strings in the modified string.
 
//echo -a $replace( 1234, 1,2, 2,3, 3,4 )
result: 4444
The 1st search pair changes all '1' into '2', modifying string to 2234
The 2nd search pair changes all '2' into '3', modifying again  to 3334
The 2nd search pair changes all '3' into '4', modifying again  to 4444
 
//echo -a $replacex(1234, 1,2, 2,3, 3,4 )
result: 2344
The 1st match against the 1st character replaces the '1' with '2', moving the pointer to the '2' following the match.
The 1st match against that new position replaces that '2' with 3, moving the pointer to the '3' following the match.
The 1st match against that new position replaces that '3' with '4', moving the pointer to the '4' following the match.
There are no more replacements, leaving the string as 2344.
 
//echo -a $replace(aaaaaa,aa,a)
result: aaa
The replacement strings do not become part of the match string for that same search/replace pair.
 
//echo -a $replace( aaaaaa,aa,a,aa,a)
result: aa
The 2nd pair sees the modified 'aaa' string and finds only 1 match.
 
//echo -a $replacex(aaaaaa,aa,a,aa,a)
result: aaa
The identical 2nd pair never has a chance to match anything because the first pair always matches anything the 2nd could match before the pointer moves forward.
 
//echo -a $replace(aaabbb,ab,)
result: aabb
Removes the 'ab' at position 3 (replaces 'ab' with $null), to make the string be 'aabb' and set the cursor at the character following the match. It makes no more replacements because there are no more 'ab' strings beginning at the new position.
 
//echo -a $replace(aaabbb,,ab)
result: aaabbb
Unlike $regsub, if searchstring is $null, no change is made.
 
//echo -a $replace(red fred red,red,x)
replaces all 'red' strings with 'x'
result: x fx x
 
//echo -a $replace(red fred red,red $+ $chr(32) ,x)
replaces only when 'red' is followed by a space
result: xfxred
 
//echo -a $replace(red fred red,$chr(32) $+ red,x)
replaces only when 'red' is preceded by a space
result: red fredx
 
$replace is faster than $regsub for simple search/replace, but for many pairs it can become slower. Removing punctuation by replacing them with spaces:
 
//var %sp $chr(32) | tokenize 32 $me $+ !?,*() | var %string $replace($1-,$chr(44),%sp,.,%sp,',%sp,',%sp,?,%sp,*,%sp,$chr(40),%sp,$chr(41),%sp ,!,%sp,;,%sp,:,%sp) | echo -a $1- / $istok(%string,$me,32)


Compatibility[edit]

Added: mIRC v4.52
Added on: 06 Jul 1996
Note: Unless otherwise stated, this was the date of original functionality.
Further enhancements may have been made in later versions.


See also[edit]