将 Range.Text 关联到 Range.Start 和 Range.End

发布于 2024-09-24 20:27:17 字数 659 浏览 4 评论 0原文

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

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

根据匹配,我想更改与纯文本相对应的格式化文本。我遇到的问题是 .Text 属性中的字符索引与 .Start.End 属性不匹配范围对象。有谁知道有什么方法可以匹配这些索引吗?

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

我可以移动正确的数字从 Document.Range().Collapse(WdCollapseStart) 开始,然后从 range.MoveStart(WdUnitChar, match.Index) 开始,因为按字符移动与格式化文本位置匹配与纯文本中的匹配项一起。

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

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; }
        ...
    }
}

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?

(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.))

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.

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2024-10-01 20:27:17

显然,我的比赛仍然失败的原因与隐藏的“Bell”字符有关(char bell = '\a';)。通过将这些替换为 Application.ActiveDocument.Range().Text 中的空字符串,我在此属性上的匹配现在与通过以下方式实现的范围正确匹配:

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

基本上,您可以在 中镜像索引.Text 属性,通过逐字符移动格式化文本。唯一需要注意的是,您需要从 .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);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文