需要Regex表达式只允许用逗号分隔的数字或字母,它不应该允许字母数字

时间:2018-02-02 06:47:33

标签: c# regex

需要正则表达式只允许数字或以逗号分隔的字母,并且不应允许字母数字组合(如"abc123")。

一些例子:

Valid:

   123,abc 
   abc,123
   123,123
   abc,abc

Invalid:

   abc,abc123   
   abc133,abc 
   abc123,abc123

3 个答案:

答案 0 :(得分:1)

由于有效和无效已更改,我已从头开始重写我的回答。 建议的模式是

^(([0-9]+)|([a-zA-Z]+))(,(([0-9]+)|([a-zA-Z]+)))*$

演示:

string[] tests = new string[] {
  "123,abc",
  "abc,123",
  "123,123",
  "abc,abc",

  "abc,abc123",
  "abc133,abc",
  "abc123,abc123",
   // More tests
  "123abc",       // invalid (digits first, then letters)
  "123",          // valid   (one item)
  "a,b,c,1,2,3",  // valid   (more than two items)  
  "1e4",          // invalid (floating point number)
  "1,,2",         // invalid (empty part)
  "-3",           // invalid (minus sign) 
  "۱۲۳",          // invalid (Persian digits)
  "число"         // invalid (Russian letters) 
};

string pattern = @"^(([0-9]+)|([a-zA-Z]+))(,(([0-9]+)|([a-zA-Z]+)))*$";

var report = string.Join(Environment.NewLine, tests
  .Select(item => $"{item,-20} : {(Regex.IsMatch(item, pattern) ? "valid" : "invalid")}"));

Console.WriteLine(report);

结果:

123,abc              : valid
abc,123              : valid
123,123              : valid
abc,abc              : valid
abc,abc123           : invalid
abc133,abc           : invalid
abc123,abc123        : invalid
123abc               : invalid
123                  : valid
a,b,c,1,2,3          : valid
1e4                  : invalid
1,,2                 : invalid
-3                   : invalid
۱۲۳                  : invalid
число                : invalid

模式的解释:

^                         - string beginning (anchor)
([0-9]+)|([a-zA-Z]+)      - either group of digits (1+) or group of letters
(,(([0-9]+)|([a-zA-Z]+))) - fllowed by zero or more such groups
$                         - string ending (anchor) 

答案 1 :(得分:0)

如果您使用RegexOptions.IgnoreCase指定Dmitry Bychenkos正则表达式,则可以将其缩小为Regex.IsMatch (test, @"^[0-9a-z](,[0-9a-z])*$",RegexOptions.IgnoreCase)

检查没有正则表达式的替代方法(表现更差):

using System;
using System.Linq;

public class Program1
{
    public static void Main()
    {
        var mydata = new[] {"1,3,4,5,1,3,a,s,r,3", "2, 4 , a", " 2,3,as"};


        // function that checks it- perfoms not as good as reges as internal stringarray
        // is build and analyzed
        Func<string,bool> isValid = 
            data => data.Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries)
            .Select(s => s.Trim())
            .All(aChar => aChar.Length == 1 && char.IsLetterOrDigit(aChar[0]));

        foreach (var d in mydata)
        {

            Console.WriteLine(string.Format("{0} => is {1}",d, isValid(d) ? "Valid" : "Invalid"));
        }
    }
} 

输出:

1,3,4,5,1,3,a,s,r,3 => is Valid
2, 4 , a => is Valid
 2,3,as => is Invalid

答案 2 :(得分:0)

匹配以逗号分隔的单词,其中单词由数字或字母组成:

^(\d+|[a-zA-Z]+)(,(\d+|[a-zA-Z]+))*$

说明

  • \d+匹配至少一位数的字符串。
  • [a-zA-Z]匹配至少一个大写或小写字母的字符串。
  • (\d+|[a-zA-Z]+)匹配一串数字或一串字母。

C#

Regex regex = new Regex(@"^(\d+|[a-zA-Z]+)(,(\d+|[a-zA-Z]+))*$");