Skip to content

Commit 6edbe02

Browse files
committed
feat: add constant for Placeholder regex pattern
1 parent 8709ce3 commit 6edbe02

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

OsmoDoc/Word/WordDocumentGenerator.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ namespace OsmoDoc.Word;
2020
/// Provides functionality to generate Word documents based on templates and data.
2121
/// </summary>
2222
public static class WordDocumentGenerator
23-
{
23+
{
24+
private const string PlaceholderPattern = @"{[a-zA-Z]+}";
25+
2426
/// <summary>
2527
/// Generates a Word document based on a template, replaces placeholders with data, and saves it to the specified output file path.
2628
/// </summary>
@@ -80,7 +82,7 @@ public async static Task GenerateDocumentByTemplate(string templateFilePath, Doc
8082
XWPFParagraph paragraph = (XWPFParagraph)element;
8183

8284
// 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))
8486
{
8587
continue;
8688
}
@@ -166,7 +168,7 @@ private static void WriteDocument(XWPFDocument document, string filePath)
166168
private static XWPFParagraph ReplacePlaceholdersOnBody(XWPFParagraph paragraph, Dictionary<string, string> textPlaceholders)
167169
{
168170
// 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)
170172
.Cast<Match>()
171173
.Select(s => s.Groups[0].Value).ToList();
172174

@@ -201,7 +203,7 @@ private static XWPFTable ReplacePlaceholderOnTables(XWPFTable table, Dictionary<
201203
foreach (XWPFParagraph paragraph in cell.Paragraphs)
202204
{
203205
// 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)
205207
.Cast<Match>()
206208
.Select(s => s.Groups[0].Value).ToList();
207209

0 commit comments

Comments
 (0)