I would like to write a regex expression that will seek for this pattern: asdf=123
and then once something is found in a file/string, it should just replace the 123
part to bbbcccddd
.
How can I do that in C# ?
I would like to write a regex expression that will seek for this pattern: asdf=123
and then once something is found in a file/string, it should just replace the 123
part to bbbcccddd
.
How can I do that in C# ?
Console.WriteLine(Regex.Replace("11asdf=123 ttt", @"(?<=asdf=)123", "321"));
Though I write this code,but I think there will many problem because you don't describe your problem clearly,like if there some character not white before asdf
,or there is some number after 123
,do you still want to replace 123
?
string pattern = @"asdf=123";
var result = Regex.Replace(yourString, pattern, "asdf=bbbcccddd");
You could do something like so:
string pattern = "asdf=123";
Regex r = new Regex(@"(.*?=)(.*)");
Match m = r.Match(pattern);
if(m.Success)
Console.WriteLine(m.Groups[1].Value + new String(m.Groups[2].Value.Reverse().ToArray()));
The above will match anything till it finds an equals ((.*?=)
) and places it in a group. It will then match what follows and places it in another group ((.*)
). The first group is then printed as is while the content of the second group is reversed.
A less verbose but less flexible way of doing this would be to use Regex.Replace
, with something like so:
string pattern = "asdf=123";
Console.WriteLine(Regex.Replace(pattern, @"(.*?=)(\d)(\d)(\d)", "$1$4$3$2"));
This is less robust though since you would need to know the amount of digits before hand.
EDIT: As per your edit:
You will need to use: Regex.Replace("asdf=123", "(.*?=)\d{3}","$1bbbcccddd")