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. Probably an easy Regex question

Probably an easy Regex question

Scheduled Pinned Locked Moved C#
questionregexhelptutoriallounge
4 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
    Andrew Stampor
    wrote on last edited by
    #1

    This is probably something easy for someone who is experienced with Regex and regular expressions in general. Unfortunately, I am not one of those people. Perhaps one of you kind souls will take pity on me and help me out here. I need to replace a part of a string with another string. I only want to replace the first instance. An example of the the beginning of the string being searched is as follows: <chart first_node_id="None" item_id="323f359f-f2db-4cd5-b779-aebc1349d587" form_type="None" name="here is an old name" last_modified="1/16/2008 11:03:33 AM"> <node name="don't change this name" node_id="ff918de6-213d-4a11-8c1c-ce5b54c7d8f2" result="None" What I want to find and replace is the "here is an old name" part. Note that 'name=' is repeated, and I only want the first one replaced. I've tried several different expressions, but the one I am currently toying with is as follows: Regex regExp = new Regex("name=\"(.*)\""); string test = regExp.Replace(oldString,newName,1); This doesn't do what I want. It finds 'name="here is an old name" last_modified="1/16/2008 11:03:33 AM"' and replaces the whole thing instead of just what is between the quotation marks. I've been trying to read up on reg expressions and Regex, but the time crunch I am under is making me impatient and it is hard to learn with that mentality. So, any help you can provide would be most appreciated. Thanks in advance.

    T M 2 Replies Last reply
    0
    • A Andrew Stampor

      This is probably something easy for someone who is experienced with Regex and regular expressions in general. Unfortunately, I am not one of those people. Perhaps one of you kind souls will take pity on me and help me out here. I need to replace a part of a string with another string. I only want to replace the first instance. An example of the the beginning of the string being searched is as follows: <chart first_node_id="None" item_id="323f359f-f2db-4cd5-b779-aebc1349d587" form_type="None" name="here is an old name" last_modified="1/16/2008 11:03:33 AM"> <node name="don't change this name" node_id="ff918de6-213d-4a11-8c1c-ce5b54c7d8f2" result="None" What I want to find and replace is the "here is an old name" part. Note that 'name=' is repeated, and I only want the first one replaced. I've tried several different expressions, but the one I am currently toying with is as follows: Regex regExp = new Regex("name=\"(.*)\""); string test = regExp.Replace(oldString,newName,1); This doesn't do what I want. It finds 'name="here is an old name" last_modified="1/16/2008 11:03:33 AM"' and replaces the whole thing instead of just what is between the quotation marks. I've been trying to read up on reg expressions and Regex, but the time crunch I am under is making me impatient and it is hard to learn with that mentality. So, any help you can provide would be most appreciated. Thanks in advance.

      T Offline
      T Offline
      TJoe
      wrote on last edited by
      #2

      You need to use a "non-greedy" or lazy flag on the .* part. So it would be .*? It is documented here[^].

      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

      A 1 Reply Last reply
      0
      • T TJoe

        You need to use a "non-greedy" or lazy flag on the .* part. So it would be .*? It is documented here[^].

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        A Offline
        A Offline
        Andrew Stampor
        wrote on last edited by
        #3

        I thought I had tried the lazy flag, but it seems to have fixed my issue. Thanks!

        1 Reply Last reply
        0
        • A Andrew Stampor

          This is probably something easy for someone who is experienced with Regex and regular expressions in general. Unfortunately, I am not one of those people. Perhaps one of you kind souls will take pity on me and help me out here. I need to replace a part of a string with another string. I only want to replace the first instance. An example of the the beginning of the string being searched is as follows: <chart first_node_id="None" item_id="323f359f-f2db-4cd5-b779-aebc1349d587" form_type="None" name="here is an old name" last_modified="1/16/2008 11:03:33 AM"> <node name="don't change this name" node_id="ff918de6-213d-4a11-8c1c-ce5b54c7d8f2" result="None" What I want to find and replace is the "here is an old name" part. Note that 'name=' is repeated, and I only want the first one replaced. I've tried several different expressions, but the one I am currently toying with is as follows: Regex regExp = new Regex("name=\"(.*)\""); string test = regExp.Replace(oldString,newName,1); This doesn't do what I want. It finds 'name="here is an old name" last_modified="1/16/2008 11:03:33 AM"' and replaces the whole thing instead of just what is between the quotation marks. I've been trying to read up on reg expressions and Regex, but the time crunch I am under is making me impatient and it is hard to learn with that mentality. So, any help you can provide would be most appreciated. Thanks in advance.

          M Offline
          M Offline
          martin_hughes
          wrote on last edited by
          #4

          Another tip for you - download Expresso[^] if you haven't already as it makes working with RegEx's much simpler.

          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