C#,如果';一封信';后跟';一封信';

C#,如果';一封信';后跟';一封信';,c#,string,c#-4.0,C#,String,C# 4.0,比如说,*后面必须跟&。 比如说, string asd = "Mother*&Mother*&Son"; // which is "Mother+ "*&" + "Mother" + "*&" + "Son" // This is correct string. 坏榜样 string asd = "Mother*Mother*&Son"; string asf = "Mother**&Mother*&Son"; string asg =

比如说,*后面必须跟&。 比如说,

string asd = "Mother*&Mother*&Son";
// which is "Mother+ "*&" + "Mother" + "*&" + "Son"
// This is correct string.
坏榜样

string asd = "Mother*Mother*&Son";
string asf = "Mother**&Mother*&Son";
string asg = "Mother*&*Mother*&Son";
如何在C#中检查字符串是否正确

编辑
根据你们介绍的正则表达式的用法,我有一个附带的问题。我实际上用逗号(,)代替了星号(*),用引号(“)代替了符号(&)。 在C#中,(让我用一个家伙的例子)

我也试过了

Regex.IsMatch("Mother,\",Mother,\"Son", @"\,(?!\")") 
//not work, neither

使用正则表达式并检查*&的匹配数是否与*s的匹配数相同

我头脑中的代码,可能无法编译,但请尝试:

Regex r = new Regex(@"\*&");
Regex r2 = new Regex(@"\*");
if (r.Matches(myString).Count == r2.Matches(myString).Count) //success!

您可以使用正则表达式,但当字符串不正确时,更容易找到,然后对结果求反

我会查找任何后面不跟
&
*
。正则表达式应该是:
(\*[^&])|(\*$)

简单测试代码:

var inputs = new[] {
    "Mother*&Mother*&Son",
    "Mother*Mother*&Son",
    "Mother**&Mother*&Son",
    "Mother*&*Mother*&Son",
    "Mother*&Mother*&Son*"
};

var regex = new Regex(@"(\*[^&])|(\*$)");

var isOK = inputs.Select(x => !regex.IsMatch(x)).ToList();

返回一个结果列表,其中包含
true
false
false
false
false
false
,通过查找任何星号(
*
)而不后跟符号(
&
)来查找失败:


对于这种情况,我倾向于直接方法,而不是使用正则表达式。这将使最多一次遍历整个字符串,这应该比正则表达式更有效

/// Return true if every instance of 'a' in the string is followed by 'b'. 
/// Also returns true if there are no instances of 'a' in the string.
/// Returns false if there exists any 'a' that is not followed by 'b'.
public static bool IsTwoCharSequence(string s, char a, char b)
{
    if(String.IsNullOrEmpty(s)) return true;
    if(s[s.Length - 1] == a) return false; // ends in a, not followed by b. Condition failed.

    int index = s.IndexOf(a); // find the first a
    while(index != -1)
    {
        if(s[index + 1] != b) return false; // a not followed by b.
        index = s.IndexOf(a, index + 1);
    }

    return true; // either no a, or all a followed by b.
}

编辑:此外,当分隔符也是正则表达式中的特殊字符时,您不需要担心如何引用它们

/// Return true if every instance of 'a' in the string is followed by 'b'. 
/// Also returns true if there are no instances of 'a' in the string.
/// Returns false if there exists any 'a' that is not followed by 'b'.
public static bool IsTwoCharSequence(string s, char a, char b)
{
    if(String.IsNullOrEmpty(s)) return true;
    if(s[s.Length - 1] == a) return false; // ends in a, not followed by b. Condition failed.

    int index = s.IndexOf(a); // find the first a
    while(index != -1)
    {
        if(s[index + 1] != b) return false; // a not followed by b.
        index = s.IndexOf(a, index + 1);
    }

    return true; // either no a, or all a followed by b.
}

编辑2:是的,它有两个循环,但看看每个循环都在做什么

内部循环,即String.IndexOf内部的循环,将遍历字符,直到找到传入的字符从字符串的开头开始搜索,后续的搜索从该索引开始,然后继续搜索到下一个匹配项,或搜索到结尾。总之,我们只对整个字符串进行了一次遍历

这里有另一个方法,它在概念上与上面的方法类似,但是“只迭代整个字符串一次”更显式

public static bool IsTwoCharSequence(string s, char a, char b)
{
    if (String.IsNullOrEmpty(s)) return true;

    bool foundA = false;

    foreach (char c in s)
    {
        if (foundA && c == b)
            foundA = false;
        else if (foundA)
            return false;
        else if (c == a)
            foundA = true;
    }

    if (foundA) return false; // 'a' was the last char in the string.

    return true;
}

感谢您的帮助!您能提供一些示例代码吗?我不擅长正则表达式..@Chimoo,或者您可以检查是否有
*
后面没有
&
。这个字符串可以有多深?可以有三对以上吗?我想我的观点是,这可能是最适合使用解析器的。@neoistheone我将使用这个字符串验证我的JSON字符串是否庞大的逻辑?这就是您所问的吗?您的新正则表达式字符串不正确。它应该是
,(?!\”
(),但在字符串末尾找不到
*
。@TimS。我已经更新了我的答案来匹配那个案例。现在应该可以工作了。这里有一个关于regex lookarounds的很好的参考,比如
(?!&)
:@canon我有一个问题。实际上,我用逗号(,)代替了星号(*),用引号(“)代替了符号(&)。在C#中,Regex.IsMatch(“Mother*&*Mother*&Son”,@“\,(?!”)不起作用。。有什么想法吗?我还尝试了Regex.IsMatch(“Mother*&*Mother*&Son”,@“\,(?!\”)@Adrian您没有更新您的输入字符串;它没有任何逗号或引号。这样,Regex字符串将是
,(?!\”,
而不是
,(?!\”)“
。如果要转义引号,则不能转义逗号,也不能使用
@
。所以,它看起来像
Regex.IsMatch(“母亲,\”,母亲,\“儿子,”,(?!\”)
。也就是说,如果我们正朝着这个方向前进,我不知道我会使用Regex进行csv解析……您的代码看起来很好,可以理解。我不知道使用Regex有多快,但这段代码似乎使用了很多循环,不是吗?(2-IndexOf,while-loop)如果我错了,请纠正我。我将使用JSON字符串,越快越好,这是一个循环,但总的来说,它只在整个字符串上迭代一次。编写良好的正则表达式可能也只在字符串上迭代一次,但每次迭代的工作量可能比这个要多。请参阅编辑以获取生成“ite”的另一个版本仅对字符串进行一次“更显式”评级。
public static bool IsTwoCharSequence(string s, char a, char b)
{
    if (String.IsNullOrEmpty(s)) return true;

    bool foundA = false;

    foreach (char c in s)
    {
        if (foundA && c == b)
            foundA = false;
        else if (foundA)
            return false;
        else if (c == a)
            foundA = true;
    }

    if (foundA) return false; // 'a' was the last char in the string.

    return true;
}