Click here to Skip to main content
14,765,915 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
I am replacing a match using regex in the whole document. But I want it to ONLY replace the match if it hasn't already been replaced. or it ends with a certain string like "done"
Posted
Updated 20-Sep-16 14:55pm
v6
Comments
Patrice T 13-Sep-16 0:19am
   
Show more code!
There is no link between RegEx operation and the doc to save
Patrice T 13-Sep-16 0:41am
   
Show an example of the replacement you want to do.
Patrice T 13-Sep-16 1:08am
   
The example do not match the code.
Garth J Lancaster 13-Sep-16 1:18am
   
There's a lot you're not showing us ...

string input = doc.ToString();
string result = Regex.Replace(input, pattern, replacement);

ok, you have a 'doc', you get its content to 'input', do a regex on it to get result .. then you go

doc.Save(dir + "\\" + file?.Replace(".xml", " - Processed.xml")); //not saving the regex changes

ok, but, where do you replace the content of 'doc' with 'result', obtained by the regex - without showing us what doc is, and outlining how you update 'doc' with 'result', I fear we cant help much - ppolymorphe has tried to explain this
Karthik_Mahalingam 13-Sep-16 1:23am
   
not clear

It is hard to tell from your question exactly what you need. Something that will help you in general is to learn more about how to use RegEx. A very helpful site is regexlib [^]they have a large library of Regular Expressions. They also have a RegEx tester[^] which is very helpful in testing your RegEx code. Also Ultrapico's Expresso[^] is a very useful tool for creating regular expressions.
   
Comments
Maciej Los 14-Sep-16 2:24am
   
Bill, please take a look at His/Her next question, which is exactly the same as current. I'm afraid, OP isn't able to explain His/Her issue.
http://www.codeproject.com/Questions/1129325/Copy-all-nodes-of-a-parent-and-append-it-in-the-pa
Please, read my comments to that question.

Cheers, Maciej
You show a vague example which do not match the RefEx code and the save code is unlinked to the above.
Remember, we are not in your head, we don't see your screen.
Asking questions is a skill[^]
   
Comments
BillWoodruff 13-Sep-16 12:05pm
   
My vote of #1: this is not a solution; it is a comment to the OP, and a vague comment at that.
Patrice T 13-Sep-16 12:10pm
   
Have a look at first version of question.
BillW33 13-Sep-16 13:44pm
   
I agree that the first version of the question was poor, however your "solution" should still be a comment on how to improve the question; it does not answer anything.
Patrice T 14-Sep-16 4:25am
   
I started with comments asking the OP to improve, but it was not efficient.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900