string sentence = "My current address is #6789, Baker Avenue (CB)".
I would like to replace #6789, Baker Avenue ( with #574, Tomson Street (.
Regular expression would be ^#.*($
How can I do this in C#?
string sentence = "My current address is #6789, Baker Avenue (CB)".
I would like to replace #6789, Baker Avenue ( with #574, Tomson Street (.
Regular expression would be ^#.*($
How can I do this in C#?
 
    
     
    
    using System;
using System.Text.RegularExpressions;
public class Program
{
    public static void Main()
    {
        string address = "My current address is #6789, Baker Avenue (CB)";
        Regex regex = new Regex("#.+\\(");
        address = regex.Replace(address, "#574, Tomson Street (");
        Console.WriteLine(address);
    }
}
You need to escape the opening bracket. Also in c# \ character must be escaped, so we need the combination \\( I have removed ^ and $ form your proposal. That characters anchor the pattern to the beginning and end of the phrase. And this is not the case.
 
    
    Try this:
string sentence = "My current address is #6789, Baker Avenue (CB)";
var result = Regex.Replace(sentence, @"#\d+,[^(]+", "#574, Tomson Street ");
The pattern is #\d+,[^(]+.
It's a hash #, followeg by at least one digit \d+, comma, and at least one character which isn't opening bracket: [^(]+
