Make words from string matching with string bold - c#

I want to make words matching in the string bold. I am using Jquery autocomplete with asp.net mvc. My following code works only if string has single word.
label = p.Name.Replace(termToSearch.ToLower(),"<b>" + termToSearch.ToLower() + "</b>"),
But doesnt work when I have 2 words matching which are at random position.
E.g When I search Gemini Oil
My Result should be id Gemini Sunflower Oil.
Any Ideas

A single line of Regex can do just that:
String term = "Gemini Oil";
String input = "Gemini Sunflower Oil.";
String result = Regex.Replace( input, String.Join("|", term.Split(' ')), #"<b>$&</b>");
Console.Out.WriteLine(result);
<b>Gemini</b> Sunflower <b>Oil</b>.

You could just split the search term on each space character and then run the replace multiple times:
var terms = termToSearch.split(' ');
foreach (var term in terms) {
p = p.Name.Replace(term.ToLower(),"<b>" + term.ToLower() + "</b>"),
}
label = p;

Related

C# Filter a word with an undefined number of spaces between charachers

For exampe:
I can create a wordt with multiple spaces for example:
string example = "**example**";
List<string>outputs = new List<string>();
string example_output = "";
foreach(char c in example)
{
example_putput += c + " ";
}
And then i can loop it to remve all spaces and add them to the outputs list,
The problem there is. I need it to work in scenario's where there are double spaces and more.
For example.
string text = "This is a piece of text for this **example**.";
I basicly want to detect AND remove 'example'
But, i want to do that even when it says e xample, e x ample or example.
And in my scenaria, since its a spamfilter, i cant just replace the spaces in the whole sentence like below, because i'd need to .Replace( the word with the exact same spaces as the user types it in).
.Replace(" ", "");
How would i achieve this?
TLDR:
I want to filter out a word with multiple spaces combinations without altering any other parts of the line.
So example, e xample, e x ample, e x a m ple
becomes a filter word
I wouldn't mind a method which could generates a word with all spaces as plan b.
You can use this regex to achieve that:
(e[\s]*x[\s]*a[\s]*m[\s]*p[\s]*l[\s]*e)
Link
Dotnet Fiddle
You could use a regex for that: e\s*x\s*a\s*m\s*p\s*l\s*e
\s means any whitespace character and the * means 0-n count of that whitespace.
Small snippet:
const string myInput = "e x ample";
var regex = new Regex("e\s*x\s*a\s*m\s*p\s*l\s*e");
var match = regex.Match(myInput);
if (match.Success)
{
// We have a match! Bad word
}
Here the link for the regex: https://regex101.com/r/VFjzTg/1
I see that the problem is to ignore the spaces in the matchstring, but not touch them anywhere else in the string.
You could create a regular expression out of your matchword, allowing arbitrary whitespace between each character.
// prepare regex. Need to do this only once for many applications.
string findword = "example";
// TODO: would need to escape special chars like * ( ) \ . + ? here.
string[] tmp = new string[findword.Length];
for(int i=0;i<tmp.Length;i++)tmp[i]=findword.Substring(i,1);
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(string.Join("\\s*",tmp));
// on each text to filter, do this:
string inp = "A text with the exa mple word in it.";
string outp;
outp = r.Replace(inp,"");
System.Console.WriteLine(outp);
Left out the escaping of regex-special-chars for brevity.
You can try regular expressions:
using System.Text.RegularExpressions;
....
// Having a word to find
string toFind = "Example";
// we build the regular expression
Regex regex = new Regex(
#"\b" + string.Join(#"\s*", toFind.Select(c => Regex.Escape(c.ToString()))) + #"\b",
RegexOptions.IgnoreCase);
// Then we apply regex built for the required text:
string text = "This is a piece of text for this **example**. And more (e X amp le)";
string result = regex.Replace(text, "");
Console.Write(result);
Outcome:
This is a piece of text for this ****. And more ()
Edit: if you want to ignore diacritics, you should modify regular expression:
string toFind = "Example";
Regex regex = new Regex(#"\b" + string.Join(#"\s*",
toFind.Select(c => Regex.Escape(c.ToString()) + #"\p{Lm}*")),
RegexOptions.IgnoreCase);
and Normalize text before matching:
string text = "This is a piece of text for this **examplé**. And more (e X amp le)";
string result = regex.Replace(text.Normalize(NormalizationForm.FormD), "");

Find string pattern

I'm trying to make an app that's looking for a string entered by a user. There will be a text file that's going to store a lot of strings and the app will be checking if the string can be found within this file and display the index of the string. In case the string can't be found, the app will look for specific patterns.
Here's an example of the text file:
This
This |
This is |
This car is #
| - one word
# - one or more words
How will the app work?
If "This" is the string entered by the user, the app will display the index of the first line (0).
If "This apple" is the string entered by the user, the app will display the index of "This |" (1).
If "This is awesome" is the string entered by the user, the app will display the index of "This is |" (2).
If "The car is blue and I like it" is the string entered by the user, the app will display the index of "This car is #" (3).
Usually, if I'm looking for a string I would use this code:
string[] grammarFile = File.ReadAllLines(#"C:\Users\user_name\Desktop\Text.txt");
int resp = Array.IndexOf(grammarFile, userString);
Console.WriteLine(resp);
The main problem is that I have no idea how I could do this for patterns.
You need a definition for a word. I will assume that a word is a consecutive string of any non-whitespace characters.
Let's define a regex that matches a single word:
var singleWordRegex = #"[^\s]+";
and a regex that matches one or more words (a sequence of non-whitespace characters, followed by a sequence of whitespace characters or the end of the string):
var oneOrMoreWordsRegex = #"([^\s]+([\s]|$)+)+";
Now you can transform each string from your textfile to a regex like this:
Regex ToRegex(this string grammarEntry)
{
var singleWordRegex = #"[^\s]+";
var oneOrMoreWordsRegex = #"([^\s]+([\s]|$)+)+";
return new Regex("^" + grammarEntry.Replace("|", singleWordRegex).Replace("#", oneOrMoreWordsRegex) + "$" );
}
and test every grammar entry like this:
var userString = ReadUserString();
string[] grammarFile = File.ReadAllLines(#"C:\Users\user_name\Desktop\Text.txt");
var resp = -1;
for(int i = 0; i < grammarFile.Length; ++i)
{
var grammarEntry = grammarFile[i];
if(grammarEntry.ToRegex().IsMatch(userString))
{
resp = i;
break;
}
}
Console.WriteLine(resp);
On a side note, if you're going to perform many matches it might be wise to save all ToRegex calls to an array as preprocessing.

Compare and extract common words between 2 strings

In ASP.NET C# and assuming I have a string contains a comma separated words:
string strOne = "word,WordTwo,another word, a third long word, and so on";
How to split then compare with another paragraph that might and might not contain these words:
string strTwo = " when search a word or try another word you may find that WordTwo is there with others";
Then how to output these common words departed with commas in a third string
string strThree = "output1, output2, output3";
To get a result like : "word, WordTwo, another word,"
You will need to split strOne by comma, and use a contains against strTwo.
Note: You can't split strTwo by space and use intersect because your items may have spaces. i.e. "another word"
string strOne = "word,WordTwo,another word, a third long word, and so on";
string strTwo = " when search a word or try another word you may find that WordTwo is there with others";
var tokensOne = strOne.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var list = tokensOne.Where(x => strTwo.Contains(x));
var result = string.Join(", ",list);
You could do something like this:
string strOne = "word,WordTwo,another word, a third long word, and so on";
string strTwo = " when search a word or try another word you may find that WordTwo is there with others";
string finalString = string.Empty;
foreach (var line in strOne.Split(","))
{
if(strTwo.Contains(line))
finalString += (line + ",");
}
finalString = finalString.Substring(0, finalString.Length - 1);
Console.WriteLine(finalString);

Get only Whole Words from a .Contains() statement

I've used .Contains() to find if a sentence contains a specific word however I found something weird:
I wanted to find if the word "hi" was present in a sentence which are as follows:
The child wanted to play in the mud
Hi there
Hector had a hip problem
if(sentence.contains("hi"))
{
//
}
I only want the SECOND sentence to be filtered however all 3 gets filtered since CHILD has a 'hi' in it and hip has a 'hi' in it. How do I use the .Contains() such that only whole words get picked out?
Try using Regex:
if (Regex.Match(sentence, #"\bhi\b", RegexOptions.IgnoreCase).Success)
{
//
};
This works just fine for me on your input text.
Here's a Regex solution:
Regex has a Word Boundary Anchor using \b
Also, if the search string might come from user input, you might consider escaping the string using Regex.Escape
This example should filter a list of strings the way you want.
string findme = "hi";
string pattern = #"\b" + Regex.Escape(findme) + #"\b";
Regex re = new Regex(pattern,RegexOptions.IgnoreCase);
List<string> data = new List<string> {
"The child wanted to play in the mud",
"Hi there",
"Hector had a hip problem"
};
var filtered = data.Where(d => re.IsMatch(d));
DotNetFiddle Example
You could split your sentence into words - you could split at each space and then trim any punctuation. Then check if any of these words are 'hi':
var punctuation = source.Where(Char.IsPunctuation).Distinct().ToArray();
var words = sentence.Split().Select(x => x.Trim(punctuation));
var containsHi = words.Contains("hi", StringComparer.OrdinalIgnoreCase);
See a working demo here: https://dotnetfiddle.net/AomXWx
You could write your own extension method for string like:
static class StringExtension
{
public static bool ContainsWord(this string s, string word)
{
string[] ar = s.Split(' ');
foreach (string str in ar)
{
if (str.ToLower() == word.ToLower())
return true;
}
return false;
}
}

Split a string in between two characters in C#

I have a string of type "24;#usernamehere,#AWRFR\user,#,#,#usernamehere"
I want to split this string on the first appearance on # and , i.e i want a string to be fetched which is inbetween these two characters.
So for the above string i want the OUTPUT as:
usernamehere
How can i split a string in between two characters using Regex function?
A simple Regex Pattern might do the job:
var pattern = new System.Text.RegularExpressions.Regex("#(?<name>.+?),");
test:
string s = #"24;#usernamehere,#AWRFR\user,#,#,#usernamehere";
pattern.Match(s).Groups["name"].Value; //usernamehere
Using Linq:
using System.Linq;
var input = #"24;#usernamehere,#AWRFR\user,#,#,#usernamehere";
You can split it with a single line:
var x = input.Split('#').Where(e => e.Contains(',')).Select(e => e.Split(',').First());
which is the same as:
var x = from e in input.Split('#')
where e.Contains(',')
select e.Split(',').First();
in both cases the result would be:
x = {"usernamehere", "AWRFR\user", "", ""}
Which is exactly an array with all substrings enclosed by # and ,.
Then if you want the first element just add .First() or do:
x.First();
You need to find the first index of '#' & ','. Then use substring method to get your required trimmed string. Read this for more details on substring method
string s = #"24;#usernamehere,#AWRFR\user,#,#,#usernamehere";
string finalString = s.Substring(s.IndexOf('#') + 1, s.IndexOf(',') - s.IndexOf('#') - 1);
Not exactly the way you asked for it, but should do what you want...
string input = #"24;#usernamehere,#AWRFR\user,#,#,#usernamehere";
string username = input.Substring(input.LastIndexOf("#") + 1);
If you wanted you could get the position of the first # and the ,
int hashPosition = input.IndexOf("#") + 1;
int commaPosition = input.IndexOf(",");
string username = input.Substring(hashPosition, commaPosition - hashPosition));

Categories