If the sentence starts with the x-ray X should be capitalized and also if the sentence starts after ending the previous sentence in x-ray X should be capital, if the sentence starts by middle in x-ray X should be small.
Input_x-ray.docx (12.7 KB)
Expected_Output_X-ray.docx (12.6 KB)
@Manasahr Please consider the following code
Paragraph para = doc.FirstSection.Body.FirstParagraph;
para.Range.Replace("x-rays", "X-rays");
para.Range.Replace(" X-rays", " x-rays");
para.Range.Replace(new Regex("\\.\\ *x-rays"), ". X-rays");
Your given code is not working in my case.
doc.StartTrackRevisions("y", DateTime.Now);
Document doc = new Document("C:\\Users\\Y\\Downloads\\AAAA.docx");
foreach (Paragraph p in doc.GetChildNodes(NodeType.Paragraph, true))
{
p.Range.Replace(new Regex("^x-ray"), "X-ray");
p.Range.Replace(" X-ray", " x-ray");
p.Range.Replace(new Regex("\\.\\ *x-ray"), ". X-ray");
}
doc.StopTrackRevisions();
string dataDir = @"C:\Users\Y\Downloads\Test_5.docx";
doc.Save(dataDir);
Above mention code is overriding the correct data also kindly help me asap.
Any update on this?
Suggested code works fine in the case that was described in the initial post. It converts “Input_x-ray.docx” to “Expected_Output_X-ray.docx”.
Please describe the conditions of your new case. What is written in the initial message does not correspond to this code anyway.
Please attach “C:\Users\Y\Downloads\AAAA.docx” as well as the expected document.
@Vadim.Saltykov Your given above code is not working in my case .
I have 3 case are there :
1.Lower case “x” is used in x-ray unless the word begins a sentence.
2.If the sentence starts with the x-ray X should be capitalized.
3. If the sentence starts after ending the previous sentence in x-ray X should be capital.
Note: If document having correct data no need of overriding it.
Kindly find the below input and expected output document.
Expeceted_output2.docx (12.9 KB)
Input1.docx (12.9 KB)
@Manasahr The approach suggested by Vadim is correct, first you need to replace all occurrences of x-ray
with lowercase and then replace required occurrences with uppercase. Here is a simple code:
Document doc = new Document("C:\\Temp\\Input1.docx");
// Replace all occurances of 'x-ray' and 'X-ray' with lowercase 'x-ray'.
doc.Range.Replace(new Regex("[Xx]-ray"), "x-ray");
// Replace all 'x-ray' at the beggining of paragraph with uppercase 'X-ray'
doc.Range.Replace(new Regex("^x-ray", RegexOptions.Multiline), "X-ray");
// Replace 'x-ray' after a period with uppercase 'X-ray'
FindReplaceOptions opt = new FindReplaceOptions();
opt.UseSubstitutions = true;
doc.Range.Replace(new Regex(@"([\.\?!]\s+)x-ray"), "$1X-ray", opt);
doc.Save(@"C:\Temp\out.docx");