LINQ条件不等于3个变量的串联[英] Linq condition with NOT equal for concatenation of 3 variables

本文是小编为大家收集整理的关于LINQ条件不等于3个变量的串联的处理方法,想解了LINQ条件不等于3个变量的串联的问题怎么解决?LINQ条件不等于3个变量的串联问题的解决办法?LINQ条件不等于3个变量的串联问题的解决方案?那么可以参考本文帮助大家快速定位并解决问题,译文如有不准确的地方,大家可以切到English参考源文内容。

问题描述

我确实有一个非常有帮助的问题,我需要通配符解决方案.但是,我还需要一个" 不等于".

示例:

varFirst = "ABC"
varSecond = "DEF"
varThird = "GHI"

通常,没有" 不等":

var x = vidaBiliy.SponsorPackage.Where(s => s.Policy == varFirst + varSecond + varThird ).FirstOrDefault();

所以,有人帮助我的是通配符,其中说varSecond可以是 .

.Where(s => Regex.IsMatch(s.Policy, $"^{Regex.Escape(varFirst)}.*{Regex.Escape(varThird)}$"))

在第一个和最后一个变量上与.*>.

然而,使用正则是,我如何做同样的事情varSecond一定不能等于DEF.

推荐答案

您可以使用

$"^{Regex.Escape(varFirst)}(?!{Regex.Escape(varSecond)}{Regex.Escape(varThird)}$).*{Regex.Escape(varThird)}$"

这意味着,在varFirst和varThird之间,匹配将失败,只有varSecond模式.该表达式看起来像

^ABC(?!DEFGHI$).*GHI$

请参阅 regex demo .

细节

  • ^ - 字符串的开始
  • ABC - varFirst模式
  • (?!DEFGHI$) - 否varSecond + varThird模式,然后是字符串结束
  • .* - 除了newline以外的任何0+字符
  • GHI - varThird value
  • $ - 字符串的结尾.

其他推荐答案

直截了当和简单的事物如何:

设置:

private const string Starts = "ABC";
private const string NotMiddle = "DEF";
private const string Ends = "GHI";

private const string StartMiddleEnd = "ABCDEFGHI";
private const string StartCloseEnd = "ABCXDEFGHI";
private const string StartAnyEnd = "ABCANYGHI";
private const string StartAnything = "ABCANYTHING";
private const string AnythingEnd = "ANYTHINGGHI";

private static readonly List<string> Strings = new List<string>
{
    StartMiddleEnd,
    StartCloseEnd,
    StartAnyEnd,
    StartAnything,
    AnythingEnd,
};

然后运行:

public static void Test()
{
    int startMiddleEndLength = Starts.Length + NotMiddle.Length + Ends.Length;
    var results = Strings.Where(
        s => s.StartsWith(Starts)
             && s.EndsWith(Ends)
             && (s.Length != startMiddleEndLength ||
                 s.Substring(Starts.Length, NotMiddle.Length) != NotMiddle));
    foreach (var result in results)
    {
        Debug.WriteLine(result);
    }
}

来自此的输出是:

ABCXDEFGHI
ABCANYGHI

本文地址:https://www.itbaoku.cn/post/2352618.html

问题描述

I do have a SO question that was very helpful, in which I needed a wildcard solution. However, I also need a "NOT EQUAL TO".

Example:

varFirst = "ABC"
varSecond = "DEF"
varThird = "GHI"

Normally, without an "IS NOT EQUAL":

var x = vidaBiliy.SponsorPackage.Where(s => s.Policy == varFirst + varSecond + varThird ).FirstOrDefault();

So, what someone helped me with was the wildcard in which say the varSecond could be ANYTHING.

.Where(s => Regex.IsMatch(s.Policy, $"^{Regex.Escape(varFirst)}.*{Regex.Escape(varThird)}$"))

That seems to be correct with exact matches on first and last variables and ANYTHING for the middle variable with .*.

However, with Regex, how do I do the same thing with that varSecond must NOT equal DEF.

推荐答案

You may use

$"^{Regex.Escape(varFirst)}(?!{Regex.Escape(varSecond)}{Regex.Escape(varThird)}$).*{Regex.Escape(varThird)}$"

This means, the match will fail in case between varFirst and varThird, there is only varSecond pattern. The expression looks like

^ABC(?!DEFGHI$).*GHI$

See the regex demo.

Details

  • ^ - start of string
  • ABC - varFirst pattern
  • (?!DEFGHI$) - no varSecond + varThird patterns followed by the end of string
  • .* - any 0+ chars other than a newline
  • GHI - varThird value
  • $ - end of string.

其他推荐答案

How about something straight-forward and simple like this:

Setting up:

private const string Starts = "ABC";
private const string NotMiddle = "DEF";
private const string Ends = "GHI";

private const string StartMiddleEnd = "ABCDEFGHI";
private const string StartCloseEnd = "ABCXDEFGHI";
private const string StartAnyEnd = "ABCANYGHI";
private const string StartAnything = "ABCANYTHING";
private const string AnythingEnd = "ANYTHINGGHI";

private static readonly List<string> Strings = new List<string>
{
    StartMiddleEnd,
    StartCloseEnd,
    StartAnyEnd,
    StartAnything,
    AnythingEnd,
};

and then running:

public static void Test()
{
    int startMiddleEndLength = Starts.Length + NotMiddle.Length + Ends.Length;
    var results = Strings.Where(
        s => s.StartsWith(Starts)
             && s.EndsWith(Ends)
             && (s.Length != startMiddleEndLength ||
                 s.Substring(Starts.Length, NotMiddle.Length) != NotMiddle));
    foreach (var result in results)
    {
        Debug.WriteLine(result);
    }
}

The output from this is:

ABCXDEFGHI
ABCANYGHI
查看更多