Difference between revisions of "TextReplace"

(Documented the re parameter)
Line 1: Line 1:
 
[[category:Text Functions]]
 
[[category:Text Functions]]
[[Category:Doc Status D]] <!-- For Lumina use, do not change -->
 
 
   
 
   
== TextReplace(text, pattern, subst'', all, caseInsensitive'') ==
+
== TextReplace(text, pattern, subst'', all, caseInsensitive, re'') ==
  
 
Replaces the occurrence of «pattern» in «text» by «subst».
 
Replaces the occurrence of «pattern» in «text» by «subst».
Line 10: Line 9:
 
If «caseInsensitive» is <code>True</code>, matches to «pattern» in a case-insensitive fashion.
 
If «caseInsensitive» is <code>True</code>, matches to «pattern» in a case-insensitive fashion.
  
== Library ==
+
When «re» is <code>True</code>, then «pattern» is treated as a [[regular expression]]. You can then substitute matching sub-patterns into the result using the following escape sequences in «subst»:
Text Functions
+
: <code>\0</code> = The matched text
 +
: <code>\1</code>, <code>\2</code>, ..., <code>\9</code> = Text matching the numbered sub-patterns
 +
: <code><name></code> = text matching a named sub-pattern.
  
 
== Examples ==
 
== Examples ==
:<code>TextReplace("One for one and one for all", "one", "two") &rarr; </code>
+
 
 +
:<code>[[TextReplace]]("One for one and one for all", "one", "two") &rarr; </code>
 
:<code>"One for two and one for all"</code>  
 
:<code>"One for two and one for all"</code>  
  
:<code>TextReplace("One for one and one for all", "one", "two", caseInsensitive: True) &rarr;</code>
+
:<code>[[TextReplace]]("One for one and one for all", "one", "two", caseInsensitive: True) &rarr;</code>
 
:<code>"two for one and one for all"</code>
 
:<code>"two for one and one for all"</code>
 
   
 
   
:<code>TextReplace("One for one and one for all", "one", "two", all: True) &rarr;</code>  
+
:<code>[[TextReplace]]("One for one and one for all", "one", "two", all: True) &rarr;</code>  
 
:<code>"One for two and two for all"</code>  
 
:<code>"One for two and two for all"</code>  
  
:<code>TextReplace("One for one and one for all", "one", "two", all: True, caseInsensitive: True) &rarr;</code>
+
:<code>[[TextReplace]]("One for one and one for all", "one", "two", all: True, caseInsensitive: True) &rarr;</code>
 
:<code>"two for two and two for all"</code>
 
:<code>"two for two and two for all"</code>
 +
 +
:<code>[[TextReplace]]( "The date is 01/26/2016", "(\d?\d)[/-](\d?\d)[/-](\d\d\d\d)", "\2-\1-\3",re:true,all:true)</code> &rarr;
 +
:<code>"The date is 26-01-2016"</code>
 +
 +
:<code>[[TextReplace]]( "The date is 01/26/2016", "(?<D>\d?\d)[/-](?<M>\d?\d)[/-](?<Y>\d\d\d\d)", "<M>-<D>-<Y>",re:true,all:true)</code> &rarr;
 +
:<code>"The date is 26-01-2016"</code>
  
 
== See Also ==
 
== See Also ==

Revision as of 01:59, 27 January 2016


TextReplace(text, pattern, subst, all, caseInsensitive, re)

Replaces the occurrence of «pattern» in «text» by «subst».

If «all» is omitted or False, replaces only the first occurrence. If «all» is True, replaces every occurrence of «pattern».

If «caseInsensitive» is True, matches to «pattern» in a case-insensitive fashion.

When «re» is True, then «pattern» is treated as a regular expression. You can then substitute matching sub-patterns into the result using the following escape sequences in «subst»:

\0 = The matched text
\1, \2, ..., \9 = Text matching the numbered sub-patterns
<name> = text matching a named sub-pattern.

Examples

TextReplace("One for one and one for all", "one", "two") →
"One for two and one for all"
TextReplace("One for one and one for all", "one", "two", caseInsensitive: True) →
"two for one and one for all"
TextReplace("One for one and one for all", "one", "two", all: True) →
"One for two and two for all"
TextReplace("One for one and one for all", "one", "two", all: True, caseInsensitive: True) →
"two for two and two for all"
TextReplace( "The date is 01/26/2016", "(\d?\d)[/-](\d?\d)[/-](\d\d\d\d)", "\2-\1-\3",re:true,all:true)
"The date is 26-01-2016"
TextReplace( "The date is 01/26/2016", "(?<D>\d?\d)[/-](?<M>\d?\d)[/-](?<Y>\d\d\d\d)", "<M>-<D>-<Y>",re:true,all:true)
"The date is 26-01-2016"

See Also

Comments


You are not allowed to post comments.