简体   繁体   中英

Regex doesn't recognize String C#

I'm working with Regex Class. I'm trying to figure it out, how many common matches a String has in another String.

Here is the situation:

MainWindow.DetailBLL.Name = "Top Senders By Total Threat Messages"
String detailName = MainWindow.DetailBLL.Name;

Extracted from:

MainWindow = Window Class
DetailBLL = Class
Name = Variable

public String Name
{
    get { return _Name; }
    set { _Name = value; }
}

CharacterReplacement(openedFile) = "Incoming Mail Domains Top Senders By Total Threat Messages"
String fileName = CharacterReplacement(openedFile); 

Extracted from:

OpenFileDialog openedFile = new OpenFileDialog();

Incoming_Mail_Domains_Top_Senders_by_Graymail_Messages_RawData.csv

private String CharacterReplacement(OpenFileDialog file)
{
    String input = file.SafeFileName;
    String output = input.Replace("_", " ").Replace("RawData", " ").Replace("by",      "By").Replace(".csv", " ");

    //output: "Incoming Mail Domains Top Senders By Graymail Messages"
    return output;
}

This method takes the file's name (The name of a .csv file) and convert it to a readable String, returning it as is depicted.

The use of the Regex Class:

String source = detailName;

String searchPattern = fileName;

1st try: Doesn't work

int count = Regex.Matches(searchPattern, source).Count;

or doesn't work

int count = Regex.Matches(fileName, detailName).Count;

if (count > 0)
{
    System.Windows.MessageBox.Show("Match!");
}

2nd try: Doesn't work

foreach (Match match in Regex.Matches(fileName, detailName))

or doesn't work

foreach (Match match in Regex.Matches(searchPattern, source))
{
    System.Windows.MessageBox.Show("Matches: " + counter++);
}

I've noticed something, Regex doesn't work like this way. There's no recognition on the variables:

String source = detailName;

String searchPattern = fileName;

Only works when the variables are like this:

String source = "Top Senders By Total Threat Messages";

String searchPattern = "Incoming Mail Domains Top Senders By Total Threat Messages";

But, this won't work for me, I need them to evaluate as a implicit (Non-Literal) String, not as a explicit (Literal) one, cause the variables change everytime.

There's a way to get to it please?

Well, first of all - you probably do not need regex (still I recommend to read about regex https://www.regular-expressions.info/ ).

I guess that you need to count how many words are contained in both strings. What your comments neither question says is if you want to count the same word twice or just once.

Here you can find basic sample:

using System;
using System.Linq;

namespace SearchLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            string source = "Top Senders By Total Threat Messages";
            string find = "Incoming Mail Domains Top Senders By Total Threat Messages";

            // first possible solution
            int result = 0;
            foreach (string word in find.Split(' '))
            {
                if (source.Contains(word))
                {
                    result++;
                }
            }
            Console.WriteLine(result);

            // second possible solution
            int result2 = find.Split(' ').Count(w => source.Contains(w));
            Console.WriteLine(result2);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM