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!

7
  • Please, try regexstorm.net/tester before coding your regex. You can fix it there. Commented Dec 23, 2015 at 13:02
  • Any sample of expected input and output + the actual output that you get? Commented Dec 23, 2015 at 13:02
  • 1
    Possible duplicate of C# dollar problem with regex-replace Commented Dec 23, 2015 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. Commented Dec 23, 2015 at 13:16
  • 1
    you can use this method to escape special characters in regex: msdn.microsoft.com/en-us/library/… Commented Dec 23, 2015 at 13:22

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

3 Comments

This won't work correctly if the ****** contains any dollar signs.
@JLRishe "******" contains only *s - that's what the UserName group should be replaced with.
I was figuring that ****** was OP's way of hiding the actual value from us, but you might be right.
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, "://" + "*****");

1 Comment

but if your user name is same as some folder's name in the path that will be replaced too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.