Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I replace all occurance of a string except when it's enclosed with a special tag?

How can I replace all occurance of a string except when it's enclosed with a special tag?

Scheduled Pinned Locked Moved C#
questionhtmlcomhelp
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AngryC
    wrote on last edited by
    #1

    Hello, Let's say I have a variable named: someVar which contains the following: href wow nice this <a href="cp.com">cow duck john</a> text etc And I want to replace: "href" with "link". I can't do: someVar.Replace("href", "link"); Because that will change: <a href="cp.com">! So, how can I replace all the occurances of "href" when it's not part of an HTML tag? Please help.

    W N 2 Replies Last reply
    0
    • A AngryC

      Hello, Let's say I have a variable named: someVar which contains the following: href wow nice this <a href="cp.com">cow duck john</a> text etc And I want to replace: "href" with "link". I can't do: someVar.Replace("href", "link"); Because that will change: <a href="cp.com">! So, how can I replace all the occurances of "href" when it's not part of an HTML tag? Please help.

      W Offline
      W Offline
      wajih boukaram
      wrote on last edited by
      #2

      hi for something simple like that just try this: someVar = someVar.Replace("

      1 Reply Last reply
      0
      • A AngryC

        Hello, Let's say I have a variable named: someVar which contains the following: href wow nice this <a href="cp.com">cow duck john</a> text etc And I want to replace: "href" with "link". I can't do: someVar.Replace("href", "link"); Because that will change: <a href="cp.com">! So, how can I replace all the occurances of "href" when it's not part of an HTML tag? Please help.

        N Offline
        N Offline
        Nadia Monalisa
        wrote on last edited by
        #3

        Try this REGEX, I hope it will be useful. You can use any variable instead of link word in the following snippet. string ResultString = null; try { ResultString = Regex.Replace(someVar, "(>.*?)(href)(.*?<)", "$1link$3", RegexOptions.Singleline | RegexOptions.IgnoreCase | RegexOptions.Multiline); } catch (ArgumentException ex) { // Syntax error in the regular expression }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups