作为参考,此代码来自对这个问题的回答:
突出显示颜色与 RichTextBox 文本中所有其他选择不同的单词或短语?
using System.Collections.Generic;
using System.Drawing;
using System.Text.RegularExpressions;
using System.Windows.Forms;
private class TextSearcher
{
private BindingSource m_bsMatches = null;
private RichTextBox m_Rtb = null;
public TextSearcher(RichTextBox rtb) : this(rtb, Color.Yellow, Color.Red) { }
public TextSearcher(RichTextBox rtb, Color selectionColor, Color currentColor)
{
this.m_Rtb = rtb;
SelectionColor = selectionColor;
CurrentColor = currentColor;
}
public string CurrentKeywords { get; private set; } = string.Empty;
public bool CaseSensitive { get; set; } = true;
public int CurrentIndex => m_bsMatches.Position;
public Match CurrentMatch => (Match)m_bsMatches.Current;
public MatchCollection Matches { get; private set; }
public Color SelectionColor { get; set; }
public Color CurrentColor { get; set; }
public void GotoMatch(int position)
{
SelectText(false);
this.m_bsMatches.Position = Math.Max(Math.Min(this.m_bsMatches.Count, position), 0);
SelectText(true);
}
public void NextMatch()
{
SelectText(false);
if (this.m_bsMatches != null && m_bsMatches.Position == this.m_bsMatches.Count - 1)
{
this.m_bsMatches.MoveFirst();
}
else
{
this.m_bsMatches.MoveNext();
}
SelectText(true);
}
public void PreviousMatch()
{
SelectText(false);
if (this.m_bsMatches != null && this.m_bsMatches.Position > 0)
{
this.m_bsMatches.MovePrevious();
}
else
{
this.m_bsMatches.MoveLast();
}
SelectText(true);
}
public int Search(string keywords)
{
if (CurrentKeywords.Equals(keywords)) return Matches.Count;
this.m_bsMatches?.Dispose();
CurrentKeywords = keywords;
var options = RegexOptions.Multiline |
(CaseSensitive ? RegexOptions.IgnoreCase : RegexOptions.None);
Matches = Regex.Matches(this.m_Rtb.Text, keywords, options);
if (Matches != null)
{
this.m_Rtb.SelectAll();
this.m_Rtb.SelectionColor = this.m_Rtb.ForeColor;
this.m_Rtb.SelectionStart = 0;
this.m_bsMatches = new BindingSource(Matches, null);
SelectKeywords();
return Matches.Count;
}
return 0;
}
private void SelectKeywords()
{
foreach (Match m in Matches)
{
SelectText(false);
NextMatch();
}
this.m_bsMatches.MoveFirst();
}
private void SelectText(bool current)
{
this.m_Rtb.Select(CurrentMatch.Index, CurrentMatch.Length);
this.m_Rtb.SelectionColor = current ? CurrentColor : SelectionColor;
}
}
Run Code Online (Sandbox Code Playgroud)
使用 :
numericUpDown1.Maximum = rbsearcherhl.Search(textBox1.Text);
Run Code Online (Sandbox Code Playgroud)
例如 textBox1 中的文本"System"
但是在我的搜索中,当您要搜索添加的多个单词时,,
,例如System,,public
或system,,public,,World
但是在 TextSearcher 调用的 Search 函数中搜索多个单词,使用的是符号 |
我怎样才能在搜索器中更改,使符号像我的一样,,
?
在搜索您键入的多个单词的示例中,System|using
但我想通过键入System,,using
或System,,using,,public,,World
而是|
如何使用,,
就拿System,,public
键入您的TextBox1的用户(或任何你叫你的文本),并将其提供给TextSearcher为
Search(textbox1.Text.Replace("|", "\\|").Replace(",,", "|"))
Run Code Online (Sandbox Code Playgroud)
或者无论如何你让 TextSearcher 去
TextSearcher 使用|
正则表达式语法,在正则表达式中的意思是“或”,所以“系统|公共”的意思是“系统或公共”。您希望“系统,公共”具有相同的含义,因此您必须:
,,
用于 OR 而不是|
or,,
使其成为正则表达式用于 OR ( |
)在这两者中,我认为后者更容易!还转换了现有 | 在替换 ,, 之前被转义