将 Range.Text 关联到 Range.Start 和 Range.End
我使用正则表达式来搜索以下属性返回的纯文本:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然,我的比赛仍然失败的原因与隐藏的“Bell”字符有关(
char bell = '\a';
)。通过将这些替换为Application.ActiveDocument.Range().Text
中的空字符串,我在此属性上的匹配现在与通过以下方式实现的范围正确匹配:基本上,您可以在
中镜像索引.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 insideApplication.ActiveDocument.Range().Text
, my matches on this property now match up correctly with the range achieved by: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.