Как заменить текст, между которым есть разрывы строк, используя С#.net? ⇐ C#
-
Anonymous
Как заменить текст, между которым есть разрывы строк, используя С#.net?
I'm trying to replace certain text that is inside curly brackets {{mytext}} in a word document, using c#. But it only works with some of the text.
My word file looks like this:
This is a test {{3.3 iwanttochangethis}} But when i print it out using the code belov, the text is in multiple lines like this:
This is a test {{ 3.3 i want to change this }} Code:
using (WordprocessingDocument doc = WordprocessingDocument.Open(outputPath, true)) { var docText = doc.MainDocumentPart.Document.Body.Descendants(); foreach (var text in docText) { if (text.Text.Contains("3.3 iwanttochangethis")) { text.Text = text.Text.Replace("3.3 iwanttochangethis", "To this"); } } } I've tried replacing the linebreaks with \r\n in the code but it didnt work. I've also tried regex using regex expressions: Code with linebreaks:
{{\r\n 1.1\r\n b\r\n ygv\r\n ae\r\n rkets\r\n A\r\n rt\r\n O\r\n g\r\n A\r\n nvendelse\r\n }} And regex:
string searchText = @"\{\{([\s\S]*?)\}\}"; It has not problem finding and replacing text that it does register to be on the same line.
In XML, for everytime there is a new line there is a new of these, just with the line of the text inside:
Источник: https://stackoverflow.com/questions/781 ... -sharp-net
I'm trying to replace certain text that is inside curly brackets {{mytext}} in a word document, using c#. But it only works with some of the text.
My word file looks like this:
This is a test {{3.3 iwanttochangethis}} But when i print it out using the code belov, the text is in multiple lines like this:
This is a test {{ 3.3 i want to change this }} Code:
using (WordprocessingDocument doc = WordprocessingDocument.Open(outputPath, true)) { var docText = doc.MainDocumentPart.Document.Body.Descendants(); foreach (var text in docText) { if (text.Text.Contains("3.3 iwanttochangethis")) { text.Text = text.Text.Replace("3.3 iwanttochangethis", "To this"); } } } I've tried replacing the linebreaks with \r\n in the code but it didnt work. I've also tried regex using regex expressions: Code with linebreaks:
{{\r\n 1.1\r\n b\r\n ygv\r\n ae\r\n rkets\r\n A\r\n rt\r\n O\r\n g\r\n A\r\n nvendelse\r\n }} And regex:
string searchText = @"\{\{([\s\S]*?)\}\}"; It has not problem finding and replacing text that it does register to be on the same line.
In XML, for everytime there is a new line there is a new of these, just with the line of the text inside:
Источник: https://stackoverflow.com/questions/781 ... -sharp-net
Мобильная версия