1

I want to match sentence from long text. I already did like below but problem is as you can see in example 1 and example 2 I am using Contains() method and it match a single letter also a whole sentence.

But I only want to match sentence. So in example 2 the if statement should fail but currently its returning true and hitting the Console.WriteLine().

Any idea how can it be only match exact sentence?

string input = "how to make money. I love you. I want do make something";
string cleaned_input = input.Trim().ToLower().Replace(".", "");

// Example 1
if (cleaned_input.Contains("make something"))
{
    Console.WriteLine("Hit this if sentence match not a word or letter match");
}

// Example 2
if (cleaned_input.Contains("m"))
{
    Console.WriteLine("Hit this if sentence match not a word or letter match");
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
John
  • 11
  • 1
  • Sounds like you should use regular expressions. Then you can append word boundaries to the beginning and end of the search. Otherwise you'd have to add spaces before and after the search when doing the `Contains` and since it could be at the beginning or end you'd have to also check those cases along with the case that the search is equal to the input. `if(input == search || input.StartsWith(search + " ") || input.EndsWith(" " + search) || input.Contains(" " + search + " "))` – juharr Oct 25 '20 at 12:46
  • @juharr seems working fine but is there any clean solution? Like using regex? – John Oct 25 '20 at 13:08
  • `string match = "make money"; var matchesCount = Regex.Matches([Your String], $@"(?:^|\W){match}\b", RegexOptions.IgnoreCase).Count;`. Or don't ignore the case. Doesn't match punctuation – Jimi Oct 25 '20 at 13:40
  • @jimi seems your solution is best – John Oct 25 '20 at 15:10
  • @Jimi You could just use `\b` on both sides and I'd suggest doing `$@"\b{Regex.Escape(match)}\b"` to make sure any special characters that might be in `match` are escaped. – juharr Oct 25 '20 at 19:34
  • @juharr Regex.Escape() for sure, `\b...\b` I think works in Javascript (or PHP) but not in C#. – Jimi Oct 25 '20 at 19:46
  • @Jimi It works in C# too. – juharr Oct 25 '20 at 21:52
  • @juharr Yep, you're right. I don't remember why I though it wouldn't work. Probably I had a bad experience with the missing verbatim once and it stuck. – Jimi Oct 25 '20 at 22:53

1 Answers1

0

I would convert long text into list of sentences and then use Contains method

       var input = "how to make money. I love you. I want do make something.";
        
        var regex = new Regex("\\s*[A-Za-z,;'\"\\s]+[.?!]");
        
        var matches = regex.Matches(input);
        
        var sentencesMatchingStr = matches
            .Cast<Match>()
            .Select(pr => pr.Value)
            .Where(pr => pr.Contains("make something"))
            .First();
        
        if(sentencesMatchingStr == null) {
            return;
        }
        
        Console.WriteLine(sentencesMatchingStr);

.NET Fiddle

Of course there might be better way.

Józef Podlecki
  • 10,453
  • 5
  • 24
  • 50
  • That would still find a match for "m". I think the OP meant to say they want to match whole words and not sentences specifically. – juharr Oct 25 '20 at 19:37