Probably an easy Regex question
-
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. -
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. -
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
I thought I had tried the lazy flag, but it seems to have fixed my issue. Thanks!
-
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.