Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Thu Nov 05, 2015 11:20 am

Hi,

The code found the first regular expression and the replace works well, but in the array "matches" change the position
and the next iteration doesn't look for ##FECHAINFRACCION## if not "AINFRACCION## .Le c".

I use the same method in a document with format doc and works well, but with a document with format html not.

string templateHTML = "<p><strong>&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt; CAMBIAR TEXTO &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;</strong></p><p><strong>Aprecio Seguros Info:</strong> En relaci&oacute;n con la multa del vehiculo ##MATRICULA##&nbsp;con&nbsp;fecha de infracci&oacute;n&nbsp;##FECHAINFRACCION## .Le confirmamos que hemos abierto el expediente N&ordm; ##EXPEDIENTE## Mas info <strong>902123147</strong></p>";

Document document = new Document();
using (TextReader sr = new StringReader(templateHTML))
{
document.LoadHTML(sr, Spire.Doc.Documents.XHTMLValidationType.None);
}

Regex re = new Regex(@"\##\w+\##");
TextSelection[] matches = document.FindAllPattern(re);


foreach (TextSelection text in matches)
{
document.Replace(text.SelectedText, "New Text");
}

resultOuput = document.GetText();

Thanks!

Blanquita_84
 
Posts: 44
Joined: Tue Oct 13, 2015 8:58 am

Fri Nov 06, 2015 6:19 am

Hi,

Thanks for your inquiry.
Please use the Replace method as shown below.
Code: Select all
 Regex re = new Regex(@"\##\w+\##");
  document.Replace(re, "New Text");


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3047
Joined: Wed Jun 27, 2012 8:50 am

Mon Nov 09, 2015 10:58 am

This form of replacement does not help me, because I would have to use n times.
The number of replacements depends on each template.

I tried to use:

1. This form doesn't maintain the style. If the label has a style to replace, it changes the style of all the text by the style of the label.

foreach (Section section in document.Sections)
{
foreach (Paragraph p in section.Paragraphs)
{
if (p.Text != "" && p.Text.Contains("##"))
{
p.Replace(p.Text, plantillaBO.ProcessTemplateText(p.Text, context, parameters), false, false);
}
}
}

2.This method works but is inefficient, because all the process is made label to label.

Regex re = new Regex(@"\##\w+\##");

TextSelection[] _matches = document.FindAllPattern(re);
List<String> matches = _matches.Select(x => x.SelectedText).ToList<String>();

foreach (String text in matches)
{
document.Replace(text, plantillaBO.ProcessTemplateText(text, matches, notificacion, userId), false, false);
}

I need a method that can send the string array with the values translated.

Blanquita_84
 
Posts: 44
Joined: Tue Oct 13, 2015 8:58 am

Tue Nov 10, 2015 7:03 am

Hi,

Please use this solution.
Code: Select all
Regex re = new Regex(@"\##\w+\##");
            TextSelection[] matches = document.FindAllPattern(re);
            foreach (TextSelection match in matches)
            {               
                TextRange range = match.GetAsOneRange(true);
                range.Text = plantillaBO.ProcessTemplateText(text, matches, notificacion, userId);
            }


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3047
Joined: Wed Jun 27, 2012 8:50 am

Tue Nov 10, 2015 4:56 pm

1.in the following code, it can be seen that all the process is made label to label,
because if the array "matches" is ordered fails to process the template or I use Distinct some labels are not processed.

Is there some method that process all the labels in one call, and not label to label?

Regex re = new Regex(@"\##\w+\##");
TextSelection[] matches = document.FindAllPattern(re);
matches = matches != null && matches.Length >0 ? matches.Where(x => x.SelectedText != "##N##").ToArray() : null;
if (matches != null)
{
string matchValue = string.Empty;
Dictionary<string, string> processedMatches = new Dictionary<string, string>();
foreach (TextSelection match in matches)
{
TextRange range = match.GetAsOneRange(true);

matchValue = match.SelectedText;
if (!processedMatches.ContainsKey(matchValue))
{
range.Text = plantillaBO.ProcessTemplateText(match.SelectedText, matches, context, userId);
processedMatches.Add(matchValue, range.Text);
}
else
{
string value;
if (processedMatches.TryGetValue(matchValue, out value))
{
range.Text = value;
}
}
}
}

Blanquita_84
 
Posts: 44
Joined: Tue Oct 13, 2015 8:58 am

Wed Nov 11, 2015 3:53 am

Hi,

Sorry that there is no method to process all the labels in one call if you replace different values for each matching, and it only can
traverse every match to do the replacement.

If you replace the same values for all matchings, the best method as below.
Code: Select all
Regex re = new Regex(@"\##\w+\##");
  document.Replace(re, "New Text");


Best Regards,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3047
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc