将Range.Text关联到Range.Start和Range.End [英] Correlate Range.Text to Range.Start and Range.End

查看:127
本文介绍了将Range.Text关联到Range.Start和Range.End的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用正则表达式来搜索以下属性返回的纯文本:

I'm using regular expressions to search against the plain text returned by the following property:

namespace Microsoft.Office.Interop.Word
{
    public class Range
    {
        ...
        public string Text { get; set; }
        ...
    }
}

基于匹配,我想对与纯文本相对应的格式化文本进行更改.我的问题是.Text属性中的字符索引与Range对象的.Start.End属性不匹配.有谁知道将这些索引相匹配的方法吗?

Based upon the matches I want to make changes to the formatted text that corresponds to the plain text. The problem I have is that the indices of characters in the .Text property do not match up with the .Start and .End properties of the Range object. Does anyone know any way to match these indices up?

(我无法使用Word通配符查找功能(作为.NET正则表达式的替代),因为它们对于我正在搜索的模式(非贪婪的运算符等)的功能还不够强大)

(I can't use the Word wildcard find capabilities (as a replacement for .NET regular expressions) because they aren't powerful enough for the patterns I'm searching (non-greedy operators etc.))

我可以先从Document.Range().Collapse(WdCollapseStart)然后是range.MoveStart(WdUnitChar, match.Index)来移动正确数量的字符,因为按字符移动将格式文本位置与纯文本中的匹配位置相匹配.

I can move the correct number of characters by starting with Document.Range().Collapse(WdCollapseStart)and then range.MoveStart(WdUnitChar, match.Index) since moving by characters matches the formatted text position up with the matches in the plain text.

我现在的问题是,在格式化的文本中,我总是相距太远4个字符...所以也许这与其他故事范围有关?我不确定...

My problem now is that I'm always 4 characters too far along in the formatted text...so maybe it has something to do with the other story ranges? I'm not sure...

推荐答案

很明显,我的比赛仍然无法进行的原因与隐藏的"Bell"字符(char bell = '\a';)有关.通过用Application.ActiveDocument.Range().Text中的空字符串替换这些字符串,我在此属性上的匹配项现在可以正确地匹配以下范围:

Apparently the reason my matches were still off had to do with hidden "Bell" characters (char bell = '\a';). By replacing these with the empty string inside Application.ActiveDocument.Range().Text, my matches on this property now match up correctly with the range achieved by:

Word.Range range = activeDocument.Range();
range.Collapse(Word.WdCollapseStart);
range.MoveStart(Word.WdUnits.Character, regexMatch.Index);

基本上,您可以通过逐个字符浏览格式化的文本来镜像.Text属性中的索引.唯一需要注意的是,您需要从.Text属性中删除奇怪的字符,例如铃铛字符.

Basically you can mirror indexes in the .Text property by moving through the formatted text character-by-character. The only caveat is that you need to remove the strange characters such as the bell character from the .Text property.

这篇关于将Range.Text关联到Range.Start和Range.End的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆