简体   繁体   English

REGEX 不连续匹配字符串

[英]REGEX Matching string nonconsecutively

I'm trying to understand how to match a specific string that's held within an array (This string will always be 3 characters long, ex: 123, 568, 458 etc) and I would match that string to a longer string of characters that could be in any order (9841273 for example).我试图了解如何匹配保存在数组中的特定字符串(此字符串的长度将始终为 3 个字符,例如:123、568、458 等)并且我将该字符串与更长的字符串匹配可以按任何顺序排列(例如 9841273)。 Is it possible to check that at least 2 of the 3 characters in the string match (in this example) strMoves?是否可以检查字符串中的 3 个字符中是否至少有 2 个匹配(在本例中)strMoves? Please see my code below for clarification.请参阅下面的代码以进行澄清。

    private readonly string[] strSolutions = new string[8] { "123", "159", "147", "258", "357", "369", "456", "789" };
    Private Static string strMoves = "1823742"

    foreach (string strResult in strSolutions)
       {
            Regex rgxMain = new Regex("[" + strMoves + "]{2}");
            if (rgxMain.IsMatch(strResult))
            {
                MessageBox.Show(strResult);
            }
        }

The portion where I have designated "{2}" in Regex is where I expected the result to check for at least 2 matching characters, but my logic is definitely flawed.我在 Regex 中指定“{2}”的部分是我希望结果检查至少 2 个匹配字符的地方,但我的逻辑肯定有缺陷。 It will return true IF the two characters are in consecutive order as compared to the string in strResult.如果与 strResult 中的字符串相比,这两个字符的顺序是连续的,它将返回 true。 If it's not in the correct order it will return false.如果它的顺序不正确,它将返回 false。 I'm going to continue to research on this but if anyone has ideas on where to look in Microsoft's documentation, that would be greatly appreciated!我将继续对此进行研究,但如果有人对在 Microsoft 文档中的位置有任何想法,将不胜感激!

Correct order where it would return true: " 1 44 2 57" when matched to "123" incorrect order: " 3 57 1 8" when matched to "123" The 3 is before the 1, so it won't match.将返回 true 的正确顺序:“ 1 44 2 57” 当与“ 123”匹配时 错误顺序:“ 3 57 1 8” 当与“ 123”匹配时 3 在 1 之前,因此不会匹配。

You can use the following solution if you need to find at least two different not necessarily consecutive chars from a specified set in a longer string:如果您需要从较长字符串中的指定集合中找到至少两个不同的不一定连续的字符,则可以使用以下解决方案:

new Regex($@"([{strMoves}]).*(?!\1)[{strMoves}]", RegexOptions.Singleline)

It will look like它看起来像

([1823742]).*(?!\1)[1823742]

See the regex demo .请参阅正则表达式演示

Pattern details :图案详情

  • ([1823742]) - Capturing group 1: one of the chars in the character class ([1823742]) - 捕获组 1:字符类中的字符之一
  • .* - any zero or more chars as many as possible (due to RegexOptions.Singleline , . matches any char including newline chars) .* - 尽可能多的任何零个或多个字符(由于RegexOptions.Singleline.匹配任何字符,包括换行符)
  • (?!\\1) - a negative lookahead that fails the match if the next char is a starting point of the value stored in the Group 1 memory buffer (since it is a single char here, the next char should not equal the text in Group 1, one of the specified digits) (?!\\1) - 如果下一个字符是存储在 Group 1 内存缓冲区中的值的起点,则匹配失败的负前瞻(因为这里是单个字符,下一个字符不应等于中的文本)第 1 组,指定数字之一)
  • [1823742] - one of the chars in the character class. [1823742] - 字符类中的字符之一。

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

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