Difference between revisions of "TextReplace"

 
(4 intermediate revisions by 2 users not shown)
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 8:
 
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 ==
Line 25: Line 25:
 
:<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", "(?<M>\d?\d)[/-](?<D>\d?\d)[/-](?<Y>\d\d\d\d)", "<D>-<M>-<Y>", re: true, all: true)</code> &rarr;
 +
:<code>"The date is 26-01-2016"</code>
 +
 +
:<code>TextReplace("The date is 01/26/2016", ".*?(?<M>\d?\d)[/-](?<D>\d?\d)[/-](?<Y>\d\d\d\d).*", "<D>-<M>-<Y>", re: true, all: true)</code> &rarr;
 +
:<code>"26-01-2016"</code>
  
 
== See Also ==
 
== See Also ==
Line 37: Line 46:
 
* [[TextLowerCase]]
 
* [[TextLowerCase]]
 
* [[Regular Expressions]]
 
* [[Regular Expressions]]
 +
* [[Text functions]]

Latest revision as of 19:29, 23 March 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", "(?<M>\d?\d)[/-](?<D>\d?\d)[/-](?<Y>\d\d\d\d)", "<D>-<M>-<Y>", re: true, all: true)
"The date is 26-01-2016"
TextReplace("The date is 01/26/2016", ".*?(?<M>\d?\d)[/-](?<D>\d?\d)[/-](?<Y>\d\d\d\d).*", "<D>-<M>-<Y>", re: true, all: true)
"26-01-2016"

See Also

Comments


You are not allowed to post comments.