0

I've tried to replace user name in Git's URL, by using Regex.replace().

The reason I want to use Regex.Replace instead of string.Replace is because I want to replace only the first occurrence.

The expected result is: "https://******:adss!#&@github.com/test/test.git" The actual result is: "https://#32$3:adss!#&@github.com/test/test.git"

Unfortunately it's not replaced. The code as bellow:

class Program
{
    private static Regex reg = new Regex(@"(?i)(http|https):\/\/(?<UserName>.*):(.*?)@.*\/");
    private const string userNameGroup = "UserName";

    static void Main(string[] args)
    {
        string url = matchRgexWithUserName("https://#32$3:adss!#&@github.com/test/test.git");

        Console.WriteLine(url);
    }

    static string matchRgexWithUserName(string url)
    {
        Match match = reg.Match(url.ToString());

        string username = match.Groups[userNameGroup].Value;
        Regex r = new Regex(username);
        url = r.Replace(url,"******",1);
        return url;
    }
}

this line works well:

string username = match.Groups[userNameGroup].Value;

the problem is with these lines:

Regex r = new Regex(username);
        url = r.Replace(url,"******",1);
        return url;

I suspect the problem is with the "$". Is there other way to overcome on it? Thanks!

Roni
  • 369
  • 1
  • 7
  • 22
  • Please, try http://regexstorm.net/tester before coding your regex. You can fix it there. –  Dec 23 '15 at 13:02
  • Any sample of expected input and output + the actual output that you get? – Ian Dec 23 '15 at 13:02
  • 1
    Possible duplicate of [C# dollar problem with regex-replace](http://stackoverflow.com/questions/4338464/c-sharp-dollar-problem-with-regex-replace) – Vadim Martynov Dec 23 '15 at 13:03
  • Sorry but it's not the same problem. My problem is to replace any user name (not hard-coded) from the url. I'll update my question to explain more the problem. – Roni Dec 23 '15 at 13:16
  • 1
    you can use this method to escape special characters in regex: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.escape(v=vs.110).aspx – Arie Dec 23 '15 at 13:22
  • @Arie in this case yes, but in general case the escaped string can be found first earlier in the input. Suppose for instance that username is `http`. – Dialecticus Dec 23 '15 at 13:27
  • @Arie Can you be more detailed? How to escape the dollar sign? Not sure it can help my case. – Roni Dec 23 '15 at 13:45

2 Answers2

1

It doesn't work, because $ is a special character in regex.

To solve the problem, you can put everything before UserName and after it into groups too:

Regex reg = new Regex(@"(?<firstPart>(?i)(http|https):\/\/)(?<UserName>.*)(?<secondPart>:(.*?)@.*\/)");

Then you can use Replace to combine firstPart, "******" and secondPart - without UserName:

string result = reg.Replace(url, "${firstPart}******${secondPart}");

Basically, you match the url to a {firstPart}{UserName}{secondPart} pattern and replace it with {firstPart}******{secondPart} (removing the UserName).

Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39
0

Using Regex.Replace that way is the wrong way to go about this. Once you know the username, you can use a regular string replace:

url = url.Replace("://" + username, "://" + "*****");
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • but if your user name is same as some folder's name in the path that will be replaced too. – Roni Dec 23 '15 at 13:07