Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用通配符比较两个字符串_C#_Regex - Fatal编程技术网

C# 使用通配符比较两个字符串

C# 使用通配符比较两个字符串,c#,regex,C#,Regex,我需要比较两个字符串,其中一个使用“*”作为通配符。当我意识到正则表达式可以更快地执行任务时,我正在考虑使用迭代或递归方法。不幸的是,我对RegEx还不熟悉,不知道该怎么做 如果我发送了模式“He**o”,那么“Hello”和“He7(o)”应该返回true,而“hhlo”应该返回false。在for循环中使用char索引来比较字符串。如果出现模式char(通配符),则忽略比较并继续下一个比较 private bool Compare(string pattern, string compare

我需要比较两个字符串,其中一个使用“*”作为通配符。当我意识到正则表达式可以更快地执行任务时,我正在考虑使用迭代或递归方法。不幸的是,我对RegEx还不熟悉,不知道该怎么做


如果我发送了模式“He**o”,那么“Hello”和“He7(o)”应该返回true,而“hhlo”应该返回false。

在for循环中使用char索引来比较字符串。如果出现模式char(通配符),则忽略比较并继续下一个比较

private bool Compare(string pattern, string compare)
{
    if (pattern.Length != compare.Length)
        //strings don't match
        return false;

    for(int x = 0, x < pattern.Length, x++)
    {
        if (pattern[x] != '*')
        {
            if (pattern[x] != compare[x])
                return false;
        }
    }
    return true;
}
私有布尔比较(字符串模式,字符串比较)
{
if(pattern.Length!=比较.Length)
//字符串不匹配
返回false;
对于(int x=0,x
在for循环中使用char索引比较字符串。如果出现模式char(通配符),请忽略比较并继续进行下一个比较

private bool Compare(string pattern, string compare)
{
    if (pattern.Length != compare.Length)
        //strings don't match
        return false;

    for(int x = 0, x < pattern.Length, x++)
    {
        if (pattern[x] != '*')
        {
            if (pattern[x] != compare[x])
                return false;
        }
    }
    return true;
}
私有布尔比较(字符串模式,字符串比较)
{
if(pattern.Length!=比较.Length)
//字符串不匹配
返回false;
对于(int x=0,x
假设您的意思是
*
为单字符通配符,正则表达式模式中正确的替换为点(
):

您可能还希望使用
^
(字符串开头)和
$
(字符串结尾)锚定模式:

如果您希望它能够解析带有特殊字符的输入字符串,则可以像下面这样转义所有其他字符:

string regexPattern = String.Join(".",pattern.Split("*".ToCharArray())
                                             .Select(s => Regex.Escape(s)).ToArray());

假设您的意思是
*
为单字符通配符,则正则表达式模式中的正确替换为点(
):

您可能还希望使用
^
(字符串开头)和
$
(字符串结尾)锚定模式:

如果您希望它能够解析带有特殊字符的输入字符串,则可以像下面这样转义所有其他字符:

string regexPattern = String.Join(".",pattern.Split("*".ToCharArray())
                                             .Select(s => Regex.Escape(s)).ToArray());
使用“He..lo”生成正则表达式

这是一个不会被承认的情况

Regex r = new Regex("He..o");
string test = "Hhleo";
bool sucess = r.Match(a).Success;
Regex r = new Regex("He..o");
string test = "He7)o";
bool sucess = r.Match(a).Success;
这种情况将得到承认

Regex r = new Regex("He..o");
string test = "Hhleo";
bool sucess = r.Match(a).Success;
Regex r = new Regex("He..o");
string test = "He7)o";
bool sucess = r.Match(a).Success;
使用“He..lo”生成正则表达式

这是一个不会被承认的情况

Regex r = new Regex("He..o");
string test = "Hhleo";
bool sucess = r.Match(a).Success;
Regex r = new Regex("He..o");
string test = "He7)o";
bool sucess = r.Match(a).Success;
这种情况将得到承认

Regex r = new Regex("He..o");
string test = "Hhleo";
bool sucess = r.Match(a).Success;
Regex r = new Regex("He..o");
string test = "He7)o";
bool sucess = r.Match(a).Success;

这正是我今天在
php
中所做的。当您添加以下内容时:

if (pattern[x] != '*' && compare[x] != '*')

然后两个字符串都可以有通配符。(希望
&&
表示逻辑和类似于
php

这正是我今天在
php
中所做的。当您添加以下内容时:

if (pattern[x] != '*' && compare[x] != '*')

然后两个字符串都可以有通配符。(希望
&&
表示逻辑,就像
php
一样)

使用
He..o
。点表示任何字符请检查是否是您需要的。使用
He..o
。点表示任何字符请检查是否是您需要的。我忘了输入“e”在正则表达式中,立即重试,已编辑我的帖子I忘记在正则表达式中输入“e”,请立即重试,已编辑我的帖子