1

Please see the code below:

Match match = Regex.Match(doc.OuterXml, "\\b" + l_sOldvalue + "\\b");
if ((match.Success) && (l_bflag))
{
    string pattern = "\\b" + l_sOldvalue + "\\b";
    string l_sChangexml = Regex.Replace(doc.OuterXml, pattern, l_sNewValue);
    doc.LoadXml(l_sChangexml);
}

Here I'm using RegEx Match and Replace, but it replaces the tag names also. For me the tag name should not be replaced.

doc.outerxml --> contains complete xml file

Can any one please solve this?

John Willemse
  • 6,608
  • 7
  • 31
  • 45
Sakthi Raj
  • 11
  • 2

2 Answers2

1

You want to use a "negative lookbehind" in your Regex. E.g. if you want to make sure that you don't catch a tag like <blafasel> or </blafasel>, but all occurences of blafasel as a single word, use the following pattern:

var pattern = @"(?i)(?<![</])\bblafasel\b";

Or to use your sample:

string pattern = @"(?i)(?<![</])\b" + l_sOldvalue + @"\b";    
Match match = Regex.Match(doc.OuterXml, pattern);

if ((match.Success) && (l_bflag))
{
    string l_sChangexml = Regex.Replace(doc.OuterXml, pattern, l_sNewValue);
    doc.LoadXml(l_sChangexml);
}

Please show a sample of your XML the next time, that would make it easier.

HTH

  • Thank you very much , now it is worked for me , once again big thanks :) – Sakthi Raj Mar 13 '14 at 13:46
  • Can you please explain the regular expression used below @"(?i)(?<![])\bblafasel\b"; – Sakthi Raj Mar 13 '14 at 13:48
  • It says `(?i)` regexengine, watch out: `(?<!` I don't want to see left from my searchstring the following `[]` characters `\bblafasel\b` and my searchstring is the complete word blafasel. – Alexander Müller Mar 13 '14 at 13:59
  • Thank you , i would like to learn more examples in regular expression , can you please share me any links to learn more – Sakthi Raj Mar 13 '14 at 14:30
  • The resource that helped me most was the book [Introducing Regular Expressions](http://shop.oreilly.com/product/0636920012337.do). Its approach is very good and the samples are quick to understand. – Alexander Müller Mar 13 '14 at 14:42
  • Thank you , I think we have to but the book , do you have any free dwonloads link ? :) – Sakthi Raj Mar 13 '14 at 15:29
  • Hello , The above example is fine , but can you please update the regex and share me , in which the attribute name also should not be renamed eg : here "id" should not be renamed can you please us the solution asap – Sakthi Raj Mar 17 '14 at 06:46
  • @Muller :- can you please help me for the above mentioned comments – Sakthi Raj Mar 17 '14 at 12:30
0

hmm - removing all content could be done by removing everything between > and < or did i miss something? additional to this all tag-attributes have to be removed that can be done by deleting everything between <charactersToNextWhitespace and >.

may be (not testet it)

s/>.*?</></g
s/(<\w+)\W.*?>/\1/g;
vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
  • I used this regex , It is perfectly working @"(?i) @"(?i)(?<![])\b" + l_sOldvalue + @"\b"; – Sakthi Raj Mar 13 '14 at 14:40
  • Hello , The above example is fine , but can you please update the regex and share me , in which the attribute name also should not be renamed eg : here "id" should not be renamed can you please us the solution asap – Sakthi Raj Mar 17 '14 at 11:57
  • @SakthiRaj i do not completely understand what you want. do you only want the attribute names (eg: ``) or just or do you want jast to keep the id-attrib inclusive its valu (eg: ``)? the last one is easy, if the id attribute is always the first attribute. – vlad_tepesch Mar 17 '14 at 12:13
  • Hello, I don't want to change the attribute name eg-"id" , but attribute value can be changed eg-"10", I think we have to modify the following @"(?i)(?<![])\b" + l_sOldvalue + @"\b" – Sakthi Raj Mar 17 '14 at 12:29