简体   繁体   English

正则表达式可选匹配字母数字和符号

[英]Regex optional matching with letters numbers and symbols

I'm looking for a regex pattern with the following particulars我正在寻找具有以下详细信息的正则表达式模式

  • Case insensitive不区分大小写
  • Only 1 letter is valid at a time一次只有 1 个字母有效
  • letters be preceded by a number with an optional space字母前面是带有可选空格的数字
  • Any symbol is valid, ie % , @ , / , - etc任何符号都是有效的,即%@/-
  • It shouldn't match on 3 whole integer numbers 2342 34534 2342 .它不应该匹配 3 个整数2342 34534 2342 however 345345 / 435 or 236 545 is fine但是345345 / 435236 545
  • The pattern needs to match the complete string模式需要匹配完整的字符串
  • Multiple spaces and underscore will be cleaned up beforehand and not relevant多个空格和下划线会事先清理掉,不相关

Matches比赛

"1"
"12"
"12 34"
"12 a 34"
"1a"
"54b"
"32 c"
"43-23"
"45 c/34"
"45/34 d"
"67 / 345"
"23a / 56"
"12 / 56B"

Just to be more specific.只是为了更具体。 The following should not match以下不应该匹配

"1 3 5"
"34 a a"
"34b s"
"56/v"
"234 / a"
"234a/a"
"34b 456s"
"34b/456s"
"34b / 456s"

My current attempt is ^\\d+[\\s]?[az\\W]?[\\s]?[\\W]?[\\s]?\\d+?[az]?$我目前的尝试是^\\d+[\\s]?[az\\W]?[\\s]?[\\W]?[\\s]?\\d+?[az]?$

You can see a demo here你可以在这里看到一个演示

Although I'll most likely need more than one pattern (for the letters either side of the symbol), my main problem is trying to get the ?尽管我很可能需要多个模式(对于符号两侧的字母),但我的主要问题是试图获得? to work correctly, ie with the current pattern id expect to get more positives正常工作,即使用当前模式 ID 期望获得更多正面

Update更新

The current regex produces the following results.当前正则表达式产生以下结果。

Good list
-------------
False - 1 <= this should match
True - 12 34
True - 12 a 34
False - 1a <= this should match
True - 54b
False - 32 c <= this should match
True - 43-23
True - 45c/34
True - 45/34d
True - 67 / 345
True - 23a / 56
True - 12 / 56b

Bad list
-------------
False - 1 3 5
False - 34 a a
False - 34b s
False - 234a/a
True - 34b 456s <= these are false positives only because they have 2 letters
True - 34b/456s
True - 34b / 456s

Ignoring the false positives, which I can fix by creating 2 patterns, I'm stuck on why the current regex is not matching on those pointed out.忽略误报,我可以通过创建 2 个模式来修复,我一直在思考为什么当前的正则表达式与指出的那些不匹配。

Guessing from your description, I suggest the following pattern:根据您的描述,我建议采用以下模式:

^\d+\s?(?:\W\s?\d+(?:\s?[a-z])?|(?:\d+\s?)?[a-z]?|[a-z]\s?\W\s?\d+)$

Demo演示

Sample Code:示例代码:

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var goodList = new List<string>{"1", "12", "12 34","12 a 34", "1a", "54b", "32 c", "43-23", "45c/34", "45/34d", "67 / 345", "23a / 56", "12 / 56b"};
        var badList = new List<string>{"1 3 5", "34 a a", "34b s", "234a/a", "34b 456s", "34b/456s", "34b / 456s"};
        var pattern = @"^\d+\s?(?:\W\s?\d+(?:\s?[a-z])?|(?:\d+\s?)?[a-z]?|[a-z]\s?\W\s?\d+)$";
        var regex = new Regex(pattern);
        Console.WriteLine("Good list");
        Console.WriteLine("-------------");
        foreach (var item in goodList)
        {
            Console.WriteLine(string.Format("{0} - {1}", regex.IsMatch(item), item));
        }

        Console.WriteLine();
        Console.WriteLine("Bad list");
        Console.WriteLine("-------------");
        foreach (var item in badList)
        {
            Console.WriteLine(string.Format("{0} - {1}", regex.IsMatch(item), item));
        }
    }
}

You may try this too.你也可以试试这个。

(?im)^(?!(?:\d+\s){2}\d+\s*$)[^a-z\n]*(?:(?:\d\s?)[a-z])?[^a-z\n]*$

Demo in .NET regex tester ,,, please click tabs, [table] and [context] for more infomation of matching. .NET regex tester中的Demo,请点击标签、 [table][context]了解更多匹配信息。

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

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