@@ -20,7 +20,9 @@ namespace OsmoDoc.Word;
20
20
/// Provides functionality to generate Word documents based on templates and data.
21
21
/// </summary>
22
22
public static class WordDocumentGenerator
23
- {
23
+ {
24
+ private const string PlaceholderPattern = @"{[a-zA-Z]+}" ;
25
+
24
26
/// <summary>
25
27
/// Generates a Word document based on a template, replaces placeholders with data, and saves it to the specified output file path.
26
28
/// </summary>
@@ -80,7 +82,7 @@ public async static Task GenerateDocumentByTemplate(string templateFilePath, Doc
80
82
XWPFParagraph paragraph = ( XWPFParagraph ) element ;
81
83
82
84
// If the paragraph is empty string or the placeholder regex does not match then continue
83
- if ( paragraph . ParagraphText == string . Empty || ! new Regex ( @"{[a-zA-Z]+}" ) . IsMatch ( paragraph . ParagraphText ) )
85
+ if ( paragraph . ParagraphText == string . Empty || ! new Regex ( PlaceholderPattern ) . IsMatch ( paragraph . ParagraphText ) )
84
86
{
85
87
continue ;
86
88
}
@@ -166,7 +168,7 @@ private static void WriteDocument(XWPFDocument document, string filePath)
166
168
private static XWPFParagraph ReplacePlaceholdersOnBody ( XWPFParagraph paragraph , Dictionary < string , string > textPlaceholders )
167
169
{
168
170
// Get a list of all placeholders in the current paragraph
169
- List < string > placeholdersTobeReplaced = Regex . Matches ( paragraph . ParagraphText , @"{[a-zA-Z]+}" )
171
+ List < string > placeholdersTobeReplaced = Regex . Matches ( paragraph . ParagraphText , PlaceholderPattern )
170
172
. Cast < Match > ( )
171
173
. Select ( s => s . Groups [ 0 ] . Value ) . ToList ( ) ;
172
174
@@ -201,7 +203,7 @@ private static XWPFTable ReplacePlaceholderOnTables(XWPFTable table, Dictionary<
201
203
foreach ( XWPFParagraph paragraph in cell . Paragraphs )
202
204
{
203
205
// Get a list of all placeholders in the current cell
204
- List < string > placeholdersTobeReplaced = Regex . Matches ( paragraph . ParagraphText , @"{[a-zA-Z]+}" )
206
+ List < string > placeholdersTobeReplaced = Regex . Matches ( paragraph . ParagraphText , PlaceholderPattern )
205
207
. Cast < Match > ( )
206
208
. Select ( s => s . Groups [ 0 ] . Value ) . ToList ( ) ;
207
209
0 commit comments