// Method #region GetKeywordListDictionary /// <summary> /// 키워드 리스트 딕셔너리 구하기 /// </summary> /// <param name="regularExpression">정규식</param> /// <param name="text">텍스트</param> private KeywordListDictionary GetKeywordListDictionary(string regularExpression, string text) { KeywordListDictionary dictionary = new KeywordListDictionary(); Regex regex = new Regex(regularExpression); MatchCollection matchCollection = regex.Matches(text); foreach (Match match in matchCollection) { Keyword keyword = new Keyword(match); if (text.Substring(keyword.Index, keyword.Length + 1).Contains(">")) { continue; } string originalText = keyword.Text.Replace(quote, ""); if (originalText.Trim().Length == 0) { continue; } if (originalText.Contains(",") || originalText.Contains(":") || originalText.Contains(".")) { continue; } dictionary.AddItem(keyword); } return(dictionary); }
/// <summary> /// 키워드 리스트 구하기 /// </summary> /// <param name="dictionary">딕셔너리</param> /// <returns>키워드 리스트</returns> private List <Keyword> GetKeywordList(KeywordListDictionary dictionary) { List <Keyword> targetList = new List <Keyword>(); foreach (KeyValuePair <string, List <Keyword> > keyValuePair in dictionary) { List <Keyword> keywordList = keyValuePair.Value; foreach (Keyword keyword in keywordList) { targetList.Add(keyword); } } targetList = targetList.OrderBy(c => c.Index).ToList(); return(targetList); }
// Event Method #region searchButton_Click /// <summary> /// 조회 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void searchButton_Click(object sender, EventArgs e) { #region 정규식을 구한다. string regularExpression = this.regularExpressionTextBox.Text.Trim(); if (string.IsNullOrEmpty(regularExpression)) { MessageBox.Show(this, "정규식을 입력해 주시기 바랍니다.", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information); this.regularExpressionTextBox.Focus(); return; } #endregion #region 텍스트를 구한다. string text = this.textRichTextBox.Text.Trim(); if (string.IsNullOrEmpty(text)) { MessageBox.Show(this, "텍스트를 입력해 주시기 바랍니다.", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information); this.textRichTextBox.Focus(); return; } #endregion #region 텍스트 리치 텍스트 박스 데이터 스타일을 지운다. this.textRichTextBox.SelectAll(); this.textRichTextBox.SelectionFont = this.textRichTextBox.Font; this.textRichTextBox.SelectionColor = this.textRichTextBox.ForeColor; this.textRichTextBox.SelectionBackColor = this.textRichTextBox.BackColor; this.textRichTextBox.Select(0, 0); #endregion #region 키워드 리스트 딕셔너리를 설정한다. if (this.keywordListDictionary != null) { this.keywordListDictionary.ClearData(); } this.keywordListDictionary = GetKeywordListDictionary(regularExpression, text); #endregion #region 키워드 리스트 박스 데이터를 지운다. this.keywordListBox.Items.Clear(); #endregion #region 키워드 리스트를 설정한다. if (this.keywordList != null) { this.keywordList.Clear(); } this.keywordList = GetKeywordList(this.keywordListDictionary); #endregion #region 키워드 리스트 박스 데이터를 추가한다. foreach (Keyword keyword in this.keywordList) { this.keywordListBox.Items.Add(keyword); } #endregion #region 텍스트 리치 텍스트 박스에서 해당 단어를 표시한다. foreach (KeyValuePair <string, List <Keyword> > keyValuePair in this.keywordListDictionary) { List <Keyword> keywordList = keyValuePair.Value; foreach (Keyword keyword in keywordList) { this.textRichTextBox.Select(keyword.Index, keyword.Length); this.textRichTextBox.SelectionColor = Color.Red; this.textRichTextBox.SelectionBackColor = Color.Yellow; } } #endregion }