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. Need help in regex replace method.

Need help in regex replace method.

Scheduled Pinned Locked Moved C#
regexhelplearning
4 Posts 2 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.
  • R Offline
    R Offline
    Rohit16db
    wrote on last edited by
    #1

    Hi all, Can anybody provide me the regex to replace the '&' with '&' but the '&' character in word like '&','<' '>' etc shoud be ignored. Frankly speaking I am weak in regular expression. I would be highly thankful if somebody provide me the good learning articles on this. Thanks Rohit

    OriginalGriffO 1 Reply Last reply
    0
    • R Rohit16db

      Hi all, Can anybody provide me the regex to replace the '&' with '&' but the '&' character in word like '&','<' '>' etc shoud be ignored. Frankly speaking I am weak in regular expression. I would be highly thankful if somebody provide me the good learning articles on this. Thanks Rohit

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      It's not too complex, if you do it via a MatchEvaluator, as you will probably have to examine each case of '&' in context:

      Regex regexWithAllPossible = new Regex(@"(>|<|&|&)");
      string result = regexWithAllPossible.Replace(source, new MatchEvaluator(CheckMatch));
      ...
      private string CheckMatch(Match m)
      {
      if (m.Value == "&")
      {
      return "&";
      }
      return m.Value;
      }

      Remember to list all the possibilities in the regex! Get a copy of Expresso[^] - it decodes, creates, and tests regexs! It's free and really helps.

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      R 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        It's not too complex, if you do it via a MatchEvaluator, as you will probably have to examine each case of '&' in context:

        Regex regexWithAllPossible = new Regex(@"(>|<|&|&)");
        string result = regexWithAllPossible.Replace(source, new MatchEvaluator(CheckMatch));
        ...
        private string CheckMatch(Match m)
        {
        if (m.Value == "&")
        {
        return "&";
        }
        return m.Value;
        }

        Remember to list all the possibilities in the regex! Get a copy of Expresso[^] - it decodes, creates, and tests regexs! It's free and really helps.

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        R Offline
        R Offline
        Rohit16db
        wrote on last edited by
        #3

        Thanks dear, you really did a great job for me. I will definately look into expresso.

        OriginalGriffO 1 Reply Last reply
        0
        • R Rohit16db

          Thanks dear, you really did a great job for me. I will definately look into expresso.

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          You are welcome! Expresso is a good piece of kit - I've been using it for about a year and really wish I'd written it.

          You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          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