简体   繁体   English

正则表达式无法识别字符串C#

[英]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 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. 此方法采用文件名(.csv文件的名称)并将其转换为可读的String,如图所示将其返回。

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/ ). 好吧,首先-您可能不需要正则表达式(仍然建议您阅读正则表达式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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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