Highlighting the multiple keywords - c#

Iam trying to highlight the multiple keywords in gridview.I tried with forloop but it highlight only the first item from the array.
protected string HighlightText(string searchWord, string inputText)
{
// string[] strArray = new string[] { "Hello", "Welcome" };
string s = "d,s";
// Split string on spaces.
// ... This will separate all the words.
string[] words = s.Split(',');
for (int i = 0; i < words.Length; i++)
{
//Console.WriteLine(word);
searchWord = words[i];
Regex expression = new Regex(searchWord.Replace(" ", "|"), RegexOptions.IgnoreCase);
return expression.Replace(inputText, new MatchEvaluator(ReplaceKeywords));
}
return string.Empty;
}
Advance thanks.
This was the out put Iam getting only the keyword "d" get highlighted I need to highlight keyword "s" also...

Can you try something like this, instead of looping for keywords 1 by 1
string inputText = "this is keyword1 for test and keyword4 also";
Regex keywords = new Regex("keyword1|keyword2|keyword3|keyword4");
//keywords = keywords.Replace("|", "\b|\b"); //or use \b between keywords
foreach (Match match in keywords.Matches(inputText))
{
//get match.Index & match.Length for selection and color it
}

Related

Search and replace a string ([TableName].[ColumnName]) in a SQL query

I am parsing a SQL query and converting them to a domain-specific language. For this I am using Regex pattern match.
For e.g. if there is match like TableName.ColumnName (e.g. Customer.RecordNumber), then I need to replace this string with a $1*, if there is another column (e.g. Customer.Forename), then $2* and so on.
This is my code:
Dictionary<string, string> FieldsUsed = new Dictionary<string, string>();
string[] words = sql.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int x = 1;
string regPattern = "^" + TableAlias + ".\\w+$";
for (int i = 0; i < words.Length; ++i)
{
string word = words[i];
Regex regex = new Regex(regPattern, RegexOptions.IgnoreCase);
Match match = regex.Match(word);
if (match.Success)
{
string fldNo = "$" + x.ToString() + "*";
if (!FieldsUsed.ContainsKey(match.Value))
{
FieldsUsed.Add(match.Value, fldNo);
words[i] = fldNo;
++x;
}
}
}
This matches where there is an exact match, but when there is an expression like ISNULL(TableName.ColumnName, ''), then the Regex fails.
Could anyone please point the correct Regex which would extract the required value (i.e. TableName.FieldName) from both the scenarios above?

C# compare input with keywords

ive got the following c# code:
string textBoxInput = richTextBox1.Text;
StreamReader SentencesFile = new StreamReader(#"C:\Users\Jeroen\Desktop\School\C#\opwegmetcsharp\answersSen.txt");
string Sentence = SentencesFile.ReadLine();
List<List<string>> keywordsList = new List<List<string>>();
List<string> outputSentence = new List<string>();
while (Sentence != null)
{
string keywords = Sentence.Substring(0, Sentence.IndexOf(' '));
string sentenceString = Sentence.Substring(0, Sentence.IndexOf(' ') +1);
List<string> splitKeyword = keywords.Split(',').ToList();
keywordsList.Add(splitKeyword);
outputSentence.Add(sentenceString);
}
int similar = 0;
int totalSimilar = 0;
List<string> SplitUserInput = textBoxInput.Split(' ').ToList();
And a .txt file which contains the following:
car,bmw Do you own a BMW?
car,Tesla Do you own a Tesla?
new,house Did you buy a new house?
snow,outside Is it snowing outside?
internet,down Is your internet down?
I can't figure out how i can compare every word that a user typed in the input (richTextBox1.Text) with the keywords in the .txt file ( like car and bmw for the first sentence )
And it also has to remember the sentence that has the highest amount of "hits".
I'm really stuck and searched a lot, but somehow i can't find out how i can do this.
A lot of thanks in advance!
You can use the LINQ Contains to check if a word is found in a list. But beware because it is case sensitive as password does. Use it like this:
//assuming you already list the keyword here
List<string> keywords = new List<string>() { "keyword1", "keyword2" };
Then for each sentence, supposing in this form:
string sentence1 = "Hi, this keYWord1 present! But quite malformed";
string sentence2 = "keywoRD2 and keyWOrd1 also present here, malformed";
Note: the above sentences could be your text from RichTextBox or file, it doesn't matter. Here I only show the concept.
You can do:
string[] words = sentence1.ToLower().Split(new char[] { ' ', ',', '.' });
int counter = 0;
for (int i = 0; i < words.Length; ++i){
counter += keywords.Contains(words[i]) ? 1 : 0;
}
And you can do likewise for sentence2. Whoever gets the highest counter has the highest hits.
This might be too advanced for a 1st year student but this piece of code will work for your need. Using Regex class to do matching for you. Performance-wise it's faster (AFAIK). I used a console application to work on this as I don't think it will be hard for you to use it in a WinForms/WPF application.
string textBoxInput = "car test do bmw"; // Just a sample as I am using a console app
string[] sentences = File.ReadAllLines("sentences.txt"); // Read all lines of a text file and assign it to a string array
string[] keywords = textBoxInput.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); // Split textBoxInput by space
int[] matchArray = new int[sentences.Length];
for(int i = 0; i < sentences.Length; i++)
{
Regex regex = new Regex(#"\b(" + string.Join("|", keywords.Select(Regex.Escape).ToArray()) + #"+\b)", RegexOptions.IgnoreCase);
MatchCollection matches = regex.Matches(sentences[i]);
matchArray[i] = matches.Count;
}
int highesMatchIndex = Array.IndexOf(matchArray, matchArray.OrderByDescending(item => item).First());
Console.WriteLine("User input: " + textBoxInput);
Console.WriteLine("Matching sentence: " + sentences[highesMatchIndex]);
Console.WriteLine("Match count: " + matchArray[highesMatchIndex]);
Console.ReadLine();

How to Regex match on spaces and replace without overwriting spaces

I have a Regex match like the following code:
string[] specials = new string[] { "special1", "special2", "special3" };
for (int i = 0; i < specials.Length; i++)
{
string match = string.Format("(?:\\s)({0})(?:\\s)", specials[i]);
if (Regex.IsMatch(name, match, RegexOptions.IgnoreCase))
{
name = Regex.Replace(name, match, specials[i], RegexOptions.IgnoreCase);
break;
}
}
What I would like is to have the replace operation replace only the matching text and leave the leading and trailing space in tact. So "This is a Special1 sentence" would become "This is a special1 sentence". With the Replace statement above I get "This is aspecial1sentence".
Solution:
Based on #Jerry's comment, I changed the match to:
(\\s)({0})(\\s)
and the Replace to:
name = Regex.Replace(name, match, "$1" + specials[i] + "$3", RegexOptions.IgnoreCase);
and was able to get the desired results.
You can use a lookbehind and a lookahead to check for the spaces without including them in the match:
string[] specials = new string[] { "special1", "special2", "special3" };
for (int i = 0; i < specials.Length; i++)
{
string match = string.Format("(?<=\\s){0}(?=\\s)", specials[i]);
if (Regex.IsMatch(name, match, RegexOptions.IgnoreCase))
{
name = Regex.Replace(name, match, specials[i], RegexOptions.IgnoreCase);
break;
}
}
This way you don't have to add the spaces back in.

Extract keywords from text and exclude words

I have this function to extract all words from text
public static string[] GetSearchWords(string text)
{
string pattern = #"\S+";
Regex re = new Regex(pattern);
MatchCollection matches = re.Matches(text);
string[] words = new string[matches.Count];
for (int i=0; i<matches.Count; i++)
{
words[i] = matches[i].Value;
}
return words;
}
and I want to exclude a list of words from the return array, the words list looks like this
string strWordsToExclude="if,you,me,about,more,but,by,can,could,did";
How can I modify the above function to avoid returning words which are in my list.
string strWordsToExclude="if,you,me,about,more,but,by,can,could,did";
var ignoredWords = strWordsToExclude.Split(',');
return words.Except(ignoredWords).ToArray();
I think Except method fits your needs
If you aren't forced to use Regex, you can use a little LINQ:
void Main()
{
var wordsToExclude = "if,you,me,about,more,but,by,can,could,did".Split(',');
string str = "if you read about cooking you can cook";
var newWords = GetSearchWords(str, wordsToExclude); // read, cooking, cook
}
string[] GetSearchWords(string text, IEnumerable<string> toExclude)
{
var words = text.Split();
return words.Where(word => !toExclude.Contains(word)).ToArray();
}
I'm assuming a word is a series of non-whitespace characters.

Retrieve String Containing Specific substring C#

I am having an output in string format like following :
"ABCDED 0000A1.txt PQRSNT 12345"
I want to retreieve substring(s) having .txt in above string. e.g. For above it should return 0000A1.txt.
Thanks
You can either split the string at whitespace boundaries like it's already been suggested or repeatedly match the same regex like this:
var input = "ABCDED 0000A1.txt PQRSNT 12345 THE.txt FOO";
var match = Regex.Match (input, #"\b([\w\d]+\.txt)\b");
while (match.Success) {
Console.WriteLine ("TEST: {0}", match.Value);
match = match.NextMatch ();
}
Split will work if it the spaces are the seperator. if you use oter seperators you can add as needed
string input = "ABCDED 0000A1.txt PQRSNT 12345";
string filename = input.Split(' ').FirstOrDefault(f => System.IO.Path.HasExtension(f));
filname = "0000A1.txt" and this will work for any extension
You may use c#, regex and pattern, match :)
Here is the code, plug it in try. Please comment.
string test = "afdkljfljalf dkfjd.txt lkjdfjdl";
string ffile = Regex.Match(test, #"\([a-z0-9])+.txt").Groups[1].Value;
Console.WriteLine(ffile);
Reference: regexp
I did something like this:
string subString = "";
char period = '.';
char[] chArString;
int iSubStrIndex = 0;
if (myString != null)
{
chArString = new char[myString.Length];
chArString = myString.ToCharArray();
for (int i = 0; i < myString.Length; i ++)
{
if (chArString[i] == period)
iSubStrIndex = i;
}
substring = myString.Substring(iSubStrIndex);
}
Hope that helps.
First split your string in array using
char[] whitespace = new char[] { ' ', '\t' };
string[] ssizes = myStr.Split(whitespace);
Then find .txt in array...
// Find first element starting with .txt.
//
string value1 = Array.Find(array1,
element => element.Contains(".txt", StringComparison.Ordinal));
Now your value1 will have the "0000A1.txt"
Happy coding.

Categories