Method regexp.Replace(+ 3 overloads)
Overload
Finds and replaces all match instances of the regular expression.
public string Replace(string s, string repl = null, int maxCount = -1, Range? range = null, RXMatchFlags matchFlags = 0)
Parameters
s (string)
Subject string. Cannot be |
repl (string)
Replacement pattern.
Can consist of any combination of literal text and substitutions like |
maxCount (int)
Maximal count of replacements to make. If -1 (default), replaces all. |
range (Range?)
Start and end offsets in the subject string. If |
matchFlags (RXMatchFlags)
Options. The same options also can be set in regexp constructor's flags. Constructor's flags and matchFlags are added, which means that matchFlags cannot unset flags set by constructor. |
Returns
string
The result string. |
Exceptions
ArgumentNullException
s is |
ArgumentOutOfRangeException
Invalid range. |
ArgumentException
|
AuException
The PCRE API function pcre2_match failed. Unlikely. |
Remarks
This function is similar to System.Text.RegularExpressions.Regex.Replace.
Examples
var s = "one two22 three333 four";
var x = new regexp(@"\b(\w+?)(\d+)\b");
s = x.Replace(s, "'$2$1'");
print.it(s);
Overload(next)
Finds and replaces all match instances of the regular expression.
public int Replace(string s, string repl, out string result, int maxCount = -1, Range? range = null, RXMatchFlags matchFlags = 0)
Parameters
s (string)
Subject string. Cannot be |
repl (string)
Replacement pattern.
Can consist of any combination of literal text and substitutions like |
result (string)
The result string. Can be the same variable as the subject string. |
maxCount (int)
Maximal count of replacements to make. If -1 (default), replaces all. |
range (Range?)
Start and end offsets in the subject string. If |
matchFlags (RXMatchFlags)
Options. The same options also can be set in regexp constructor's flags. Constructor's flags and matchFlags are added, which means that matchFlags cannot unset flags set by constructor. |
Returns
int
The number of replacements made. Returns the result string through an out parameter. |
Exceptions
ArgumentNullException
s is |
ArgumentOutOfRangeException
Invalid range. |
ArgumentException
|
AuException
The PCRE API function pcre2_match failed. Unlikely. |
Remarks
This function is similar to System.Text.RegularExpressions.Regex.Replace.
Examples
var s = "one two22 three333 four";
var x = new regexp(@"\b(\w+?)(\d+)\b");
if(0 == x.Replace(s, "'$2$1'", out s)) print.it("not found");
else print.it(s);
Overload(next)
Finds and replaces all match instances of the regular expression. Uses a callback function.
public string Replace(string s, Func<RXMatch, string> replFunc, int maxCount = -1, Range? range = null, RXMatchFlags matchFlags = 0)
Parameters
s (string)
Subject string. Cannot be |
replFunc (Func<RXMatch, string>)
Callback function's delegate, eg lambda. Called for each found match. Returns the replacement. In the callback function you can use RXMatch.ExpandReplacement. |
maxCount (int)
Maximal count of replacements to make. If -1 (default), replaces all. |
range (Range?)
Start and end offsets in the subject string. If |
matchFlags (RXMatchFlags)
Options. The same options also can be set in regexp constructor's flags. Constructor's flags and matchFlags are added, which means that matchFlags cannot unset flags set by constructor. |
Returns
string
The result string. |
Exceptions
ArgumentNullException
s is |
ArgumentOutOfRangeException
Invalid range. |
ArgumentException
|
AuException
The PCRE API function pcre2_match failed. Unlikely. |
Remarks
This function is similar to System.Text.RegularExpressions.Regex.Replace.
Examples
var s = "one two22 three333 four";
var x = new regexp(@"\b(\w+?)(\d+)\b");
s = x.Replace(s, o => o.Value.Upper());
print.it(s);
Overload(top)
Finds and replaces all match instances of the regular expression. Uses a callback function.
public int Replace(string s, Func<RXMatch, string> replFunc, out string result, int maxCount = -1, Range? range = null, RXMatchFlags matchFlags = 0)
Parameters
s (string)
Subject string. Cannot be |
replFunc (Func<RXMatch, string>)
Callback function's delegate, eg lambda. Called for each found match. Returns the replacement. In the callback function you can use RXMatch.ExpandReplacement. |
result (string)
The result string. Can be the same variable as the subject string. |
maxCount (int)
Maximal count of replacements to make. If -1 (default), replaces all. |
range (Range?)
Start and end offsets in the subject string. If |
matchFlags (RXMatchFlags)
Options. The same options also can be set in regexp constructor's flags. Constructor's flags and matchFlags are added, which means that matchFlags cannot unset flags set by constructor. |
Returns
int
The number of replacements made. Returns the result string through an out parameter. |
Exceptions
ArgumentNullException
s is |
ArgumentOutOfRangeException
Invalid range. |
ArgumentException
|
AuException
The PCRE API function pcre2_match failed. Unlikely. |
Remarks
This function is similar to System.Text.RegularExpressions.Regex.Replace.
Examples
var s = "one two22 three333 four";
var x = new regexp(@"\b(\w+?)(\d+)\b");
if(0 == x.Replace(s, o => o.Value.Upper(), out s)) print.it("not found");
else print.it(s);