-1

I have file containing string "CRef" 39 times in different Lines, what i need is to change every "CRef" to "CRef1", "CRef2",..... and so on.

For Example the Input:

<CrossReferenceSource Self="CRef" AppliedFormat="uf4">Depressed people are often dependent on others emotionally and seek reassurance in ways that distance others. They may often overvalue relationships.
<CrossReferenceSource Self="CRef" AppliedFormat="uf4" Hidden="false">The cognitive symptoms of depression interfere with work. Three ways in which depression may impair work performance are (1) interpersonal relationships (depressed people are seen as irritable, pessimistic, and withdrawn); (2) productivity  

The Output Should be

<CrossReferenceSource Self="CRef1" AppliedFormat="uf4">Depressed people are often dependent on others emotionally and seek reassurance in ways that distance others. They may often overvalue relationships.
<CrossReferenceSource Self="CRef2" AppliedFormat="uf4" Hidden="false">The cognitive symptoms of depression interfere with work. Three ways in which depression may impair work performance are (1) interpersonal relationships (depressed people are seen as irritable, pessimistic, and withdrawn); (2) productivity

What i tried is to MatchCollection for all "CRef" and foreach loop on every match but the result was

<CrossReferenceSource Self="CRef12" AppliedFormat="uf4">Depressed people are often dependent on others emotionally and seek reassurance in ways that distance others. They may often overvalue relationships.
<CrossReferenceSource Self="CRef12" AppliedFormat="uf4" Hidden="false">The cognitive symptoms of depression interfere with work. Three ways in which depression may impair work performance are (1) interpersonal relationships (depressed people are seen as irritable, pessimistic, and withdrawn); (2) productivity

and so on.

This is a sample code of what i tried:

int Count = 1;
MatchCollection CRef = Regex.Matches(File, @"CRef");

foreach (var item in CRef)
{
    string cross = Regex.Replace(item.ToString(), @"CRef", "CRef" + Count.ToString());
    Count++;
    final3 = final3.Replace(item.ToString(),cross);    
}
gunr2171
  • 16,104
  • 25
  • 61
  • 88
MohamedSayed
  • 147
  • 1
  • 4
  • 14

2 Answers2

1

Check out this question regex replacement with a increasing number

To your specific case, something like this should work:

        string test = "<CrossReferenceSource Self=\"CRef\"><CrossReferenceSource Self=\"CRef\">";
        Regex match = new Regex("CRef");
        int count = 0;
        string result = match.Replace(test, delegate(Match t)
        {
            return "CRef" + count++.ToString();
        });
Community
  • 1
  • 1
Rodrigo Silva
  • 432
  • 1
  • 6
  • 18
  • using regex to manipulate XML is a really bad idea... ([or HTML, for that matter](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags)) – Thomas Levesque Apr 27 '14 at 14:24
  • I agree, but the author didn't mention the file type and he tagged the question with regex, that's why I used it. – Rodrigo Silva Apr 27 '14 at 14:25
0

If the input file is an XML file, you should process it as XML, not as plain text.

You can do something like that:

var doc = XDocument.Load("file.xml");
var attributes = doc.Root.Descendants("CrossReferenceSource")
                    .Select(e => e.Attribute("Self"))
                    .Where(a => a != null);
int count = 0;
foreach (var a in attributes)
{
    a.Value = "CRef" + (++count);
}
doc.Save("file.xml");
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758