I am currently using this regular expression in my C# / .NET Core app to parse HTTP, HTTPS & FTP urls from a markdown file:
static readonly Regex _urlRegex = new Regex(@"(((http|ftp|https):\/\/)+[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?)");
void UpdateGitHubReadme(string gitHubRepositoryName, string gitHubReadmeText)
{
var updatedMarkdown = _urlRegex.Replace(gitHubReadmeText, x => HandleRegex(x.Groups[0].Value, gitHubRepositoryName.Replace(".", "").Replace("-", "").ToLower(), "github", gitHubUser.Alias));
//handle updated markdown
}
static string HandleRegex(in string url, in string repositoryName, in string channel, in string alias)
{
//handle url
}
I am looking to update this regex to ignore URLs inside of markdown code blocks and markdown code snippets.
Example 1
The following URL should be ignored because it is inside of a code block:
` ` `
{
"name": "Brandon",
"blog" : "https://codetraveler.io"
}
` ` `
Example 2
The following URL should be ignored because it is inside of a code snippet:
`curl -I https://www.keycdn.com `