Search code examples
c#regexnofollow

How to change rel to nofollow with Regex - c#


i want to change some urls to nofollow and i also want, some urls dofollow

i try to do it with this Regex :

(<a\s*(?!.*\brel=)[^>]*)(href="https?://)((?!blogs.cc)[^"]+)"([^>]*)>

i can support one url to dofollow (in this ex:"blogs.cc")

if i want to dofollow more of one, what do i do? i try with :

(<a\s*(?!.*\brel=)[^>]*)(href="https?://)(((?!blogs.cc)[^"]+)||((?!wikipedia.org)[^"]+))"([^>]*)>

but i didn't get a correct answer

what's solution?


Solution

  • i resolved it and put my solution here for everybody who has same question.

    just do it

    (<a\s*(?!.*\brel=)[^>]*)(href="https?://)((?!(?:blogs.cc|wikipedia.org|moreUrls.com))[^"]+))"([^>]*)>
    

    C# Sample Code:

    Regex.Replace(str, "(<a\\s*(?!.*\brel=)[^>]*)(href=\"https?://)((?!(?:blogs.cc|wikipedia.org))[^\"]+)\"([^>]*)>", "<a $2$3\" $4 rel=\"nofollow\">")
    

    i hope it would be useful