0

input

        <w:r>
            <w:t>{{</w:t>
        </w:r>
        <w:proofErr w:type="spellStart"/>
        <w:r>
            <w:t>CreateDate</w:t>
        </w:r>
        <w:proofErr w:type="spellEnd"/>
        <w:r>
            <w:t>}}</w:t>
        </w:r>
        <w:r>
            <w:t>{{</w:t>
        </w:r>
        <w:proofErr w:type="spellStart"/>
        <w:r>
            <w:t>Text</w:t>
        </w:r>
        <w:proofErr w:type="spellEnd"/>
        <w:r>
            <w:t>}}</w:t>
        </w:r>

expect

        <w:r>
            <w:t>2022-09-20</w:t>
        </w:r>
        <w:r>
            <w:t>{{</w:t>
        </w:r>
        <w:proofErr w:type="spellStart"/>
        <w:r>
            <w:t>Text</w:t>
        </w:r>
        <w:proofErr w:type="spellEnd"/>
        <w:r>
            <w:t>}}</w:t>
        </w:r>

I tried pattern and C# code it will replace text between first `{{` and last `}}` not first `{{` and first `}}`
        <w:r>
            <w:t>2022-09-12</w:t>
        </w:r>
void Main()
{
    var input =@"        <w:r>
            <w:t>{{</w:t>
        </w:r>
        <w:proofErr w:type=""spellStart""/>
        <w:r>
            <w:t>CreateDate</w:t>
        </w:r>
        <w:proofErr w:type=""spellEnd""/>
        <w:r>
            <w:t>}}</w:t>
        </w:r>
        <w:r>
            <w:t>{{</w:t>
        </w:r>
        <w:proofErr w:type=""spellStart""/>
        <w:r>
            <w:t>Text</w:t>
        </w:r>
        <w:proofErr w:type=""spellEnd""/>
        <w:r>
            <w:t>}}</w:t>
        </w:r>";

    var output = Regex.Replace(input, @"\{\{.+CreateDate.+\}\}","2022-09-12", RegexOptions.Singleline
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.CultureInvariant);
    Console.WriteLine(output);
}

image

Wei Lin
  • 3,591
  • 2
  • 20
  • 52

0 Answers0