Match substring surrounded by known prefix and suffix using regex in c#
Add prefix and/or suffix to substring using regex match using c#
Example 1:
string strline = "This is 123 test string";
strline = Regex.Replace(strline, @"([\d])", "[$1]");
Console.WriteLine(strline);
Out Put: is [1][2][3] test string
Example 2:
string strline = "That's first [good] mark_test";
strline = Regex.Replace(strline, @"([^A-Za-z0-9 ])", "\\$1");
Console.WriteLine(strline);
Out Put: That\'s first \[good\] mark\_test
Example 3:
string strline = "Hello [123] Frinds";
strline = Regex.Replace(strline, @"([^\[])(\[\d+\])(.*)", "$1xxxxxxxxx $2 xxxxxxxxx$3");
Console.WriteLine(strline);
Out Put: Hello xxxxxxxxx [123] xxxxxxxxx Frinds
Note: In this example $1, $2, $3 means respective value of match group
Example 1:
string strline = "This is 123 test string";
strline = Regex.Replace(strline, @"([\d])", "[$1]");
Console.WriteLine(strline);
Out Put: is [1][2][3] test string
Example 2:
string strline = "That's first [good] mark_test";
strline = Regex.Replace(strline, @"([^A-Za-z0-9 ])", "\\$1");
Console.WriteLine(strline);
Out Put: That\'s first \[good\] mark\_test
Example 3:
string strline = "Hello [123] Frinds";
strline = Regex.Replace(strline, @"([^\[])(\[\d+\])(.*)", "$1xxxxxxxxx $2 xxxxxxxxx$3");
Console.WriteLine(strline);
Out Put: Hello xxxxxxxxx [123] xxxxxxxxx Frinds
Note: In this example $1, $2, $3 means respective value of match group
Comments
Post a Comment