Обновлено: вы можете попробовать это
string text = "Trio charged over alleged $100m money laundering syndicate at Merrylands, Guildford West";
string searchtext = "charged over";
searchtextPattern = "(?=" + Regex.Escape(searchtext) + ")";
string[] fragments= Regex.Split(text, searchtextPattern);
//fargments will have two elements here
// fragments[0] - "Trio"
// fragments[1] - "charged over alleged $100m money laundering syndicate at Merrylands, Guildford West"
, теперь вы можете снова разделить фрагмент, в котором есть текст для поиска, то есть фрагменты 1 в этом случае. см. код ниже
var stringWithoutSearchText = fragments[1].Replace(searchtext, string.Empty);
Вам необходимо проверить, содержит ли каждый фрагмент текст для поиска или нет. Вы можете сделать это с помощью фрагментов. добавить ниже проверить там
foreach (var item in fragments)
{
if (item.Contains(searchtext))
{
string stringWithoutSearchText = item.Replace(searchtext, string.Empty);
}
}
Я пытался вписать его в ваш код. Вы можете попробовать что-то вроде этого
foreach (SharedStringItem sharedString in sharedStrings)
{
string innerText = sharedString.InnerText; // This contains complete line (watch)
if (innerText.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0)
{
sharedString.RemoveAllChildren(); // Remove complete line from spreadsheet because we have to make it again as searched text needs to be highlighted
// Split the line so it will give blank for searched text and remaining line
var searchtextPattern = "(?=" + Regex.Escape(searchText) + ")";
string[] fragments = Regex.Split(innerText, searchtextPattern);
// loop through both words/line
foreach (var item in fragments)
{
if (!string.IsNullOrEmpty(item))
{
//It will check whether the item contains search string or not
if (item.Contains(searchtext))
{
// now GetRun() method called two times here
string stringWithoutSearchText = item.Replace(searchtext, string.Empty);
// in your example method argument will be "charged over"
var run = GetRun(new DocumentFormat.OpenXml.Spreadsheet.Text(" " + searchtext));
//this code will only execute for search text
if (run.RunProperties == null)
run.RunProperties = new RunProperties();
run.RunProperties.Append(new Color { Rgb = "008000" });
run.RunProperties.Append(new DocumentFormat.OpenXml.Spreadsheet.Bold());
sharedString.Append(run);
// in your example method argument will be "alleged $100m money laundering syndicate at Merrylands, Guildford West"
if (!string.IsNullOrEmpty(stringWithoutSearchText))
sharedString.Append(GetRun(new DocumentFormat.OpenXml.Spreadsheet.Text(" " + stringWithoutSearchText)));
}
else
{
//in your example method argument "will be Trio"
sharedString.Append(GetRun(new DocumentFormat.OpenXml.Spreadsheet.Text(" " + item)));
}
}
}
}
}
ваш метод GetRun будет выглядеть следующим образом
private Run GetRun(DocumentFormat.OpenXml.Spreadsheet.Text text)
{
text.Space = SpaceProcessingModeValues.Preserve;
// New Run needs to be created for each splitted line/word, run is like a row in spreadsheet
// You cannot create a single run because you need to take care of searched text as it needs to be highlighted before adding to the row
Run run = new Run();
run.Append(text);
return run;
}
case 2:
//if search text is at end
string watch = "Bitcoin ATMs Highlight Flaws in EU Money Laundering Rules";
string searchtext = "Money Laundering Rules";
//fragment of above string by using Regex.Split will be like
// fragments[0] - "Bitcoin ATMs Highlight Flaws in EU"
// fragments[1] - "Money Laundering Rules"
case 3:
//if search text is at start
string watch = "Money Laundering Rules Bitcoin ATMs Highlight Flaws in EU";
string searchtext = "Money Laundering Rules";
//fragment of above string by using Regex.Split will be like
// fragments[0] - ""
// fragments[1] - "Money Laundering Rules Bitcoin ATMs Highlight Flaws in EU"
отметьте эти три случая в приведенном выше коде
Ссылка: { ссылка }