[英]Extract text between 2 strings from word document using aspose.words in C#
I have a word document from which I need to extract a few lines of text.我有一个 word 文档,我需要从中提取几行文本。 the text i need to extract can be found in between the two strings: “must haves” and “could haves”.
我需要提取的文本可以在两个字符串之间找到:“must haves”和“could haves”。 Does anyone know what I should do to achieve this?
有谁知道我应该怎么做才能实现这一目标?
You can use IReplacingCallback to achieve what you need.您可以使用IReplacingCallback来实现您所需要的。 For example see the following code:
例如看下面的代码:
Document doc = new Document(@"C:\temp\in.docx");
FindReplaceOptions opt = new FindReplaceOptions();
opt.ReplacingCallback = new MyReplacingCallback();
Regex regex = new Regex(@"\<mytag\>(.*?)\<\/mytag\>");
doc.Range.Replace(regex, "", opt);
private class MyReplacingCallback : IReplacingCallback
{
public ReplaceAction Replacing(ReplacingArgs args)
{
Console.WriteLine(args.Match.Groups[1].Value);
return ReplaceAction.Skip;
}
}
use tika to extract text from docx... : https://www.nuget.org/packages/TikaOnDotNet.TextExtractor使用 tika 从 docx 中提取文本...: https://www.nuget.org/packages/TikaOnDotNet.TextExtractor
var str = new TikaOnDotNet.TextExtraction.TextExtractor().Extract(@"C:\Users\Inconnu\Downloads\test.docx").Text;
int pForm = str.IndexOf("must haves") + "must haves".Length;
int pTo = str.LastIndexOf("could haves");
string result = str.Substring(pForm, pTo - pForm);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.