C# 简单正则表达式 - 32 个字符,仅包含 0-9 和 af (GUID)

发布于 2024-10-04 19:17:55 字数 94 浏览 0 评论 0原文

如何在 C# 中使用正则表达式进行测试:

  • 字符串长度恰好为 32
  • 字符串仅包含 0-9 的数字和 af 的小写字母

How to test using regex in C# if:

  • length of string is exactly 32
  • string contains only numbers from 0-9 and small letters from a-f

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

征棹 2024-10-11 19:17:55

怎么样:

Regex regex = new Regex("^[0-9a-f]{32}$");
if (regex.IsMatch(text))
...

How about:

Regex regex = new Regex("^[0-9a-f]{32}$");
if (regex.IsMatch(text))
...
药祭#氼 2024-10-11 19:17:55

在 .Net 4.0 中,有一个 Guid 结构,它具有 TryParse 静态函数。
http://msdn.microsoft.com/en-us/library/system .guid.aspx

In .Net 4.0 there is Guid structure that has TryParse static function.
http://msdn.microsoft.com/en-us/library/system.guid.aspx

兰花执着 2024-10-11 19:17:55

如果您不打算使用正则表达式来实现此目的,那么这非常干净:

Func<char, bool> charValid = c => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
bool isValid = text.Length == 32 && text.All(charValid);

If you're not dead-set on using a Regex for this purpose, this is pretty clean:

Func<char, bool> charValid = c => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
bool isValid = text.Length == 32 && text.All(charValid);
南渊 2024-10-11 19:17:55

好的,这里有一个答案向您展示如何确定该字符串的长度是否为 32 个字符,并且每个字符要么是数字,要么是小写“a”到小写“f”,并使用正则表达式,另一个答案向您展示了如何使用一个简单的 Func 来扫描字符串中的字符。它们都是很好的答案,并且在技术上是正确的,但是在您的问题标题中,您明确指出了“GUID”,这打开了新的蠕虫罐头。

GUID 可以采用多种不同的字符串表示形式,您可能会遇到其中任何一种。您需要处理所有这些吗?您是否需要容纳以大括号(“{”和“}”)或圆括号开头和结尾的字符串?破折号('-')呢? 根据 MSDN,创建一个新的 GUID,并

string s = ...;
Guid g = new Guid(s);

允许使用以下字符串形式

32 个连续数字:
dddddddddddddddddddddddddddddddd

-或-

8、4、4、4 和 12 位数字组
组之间用连字符。这
可以选择包含整个 GUID
在匹配的大括号或圆括号中:
dddddddd-dddd-dddd-dddd-dddddddddddd

-或-

{dddddddd-dddd-dddd-dddd-dddddddddddd}

-或-

(dddddddd-dddd-dddd-dddd-dddddddddddd)

-或-

8、4 和 4 位数字组,以及
八组 2 位数字的子集,
每个组以“0x”为前缀或
“0X”,并用逗号分隔。这
整个 GUID 及其子集是
用匹配的大括号括起来:{0xdddddddd, 0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}

您需要处理所有这些情况吗?另外,请考虑使用正则表达式是否真的是最好的选择。正如一些人已经评论的那样,正则表达式可能会让一些开发人员感到困惑,而且意图并不总是明确的。最重要的是,在某些情况下,正则表达式可能会

我对三种不同的方法进行了快速性能测试,以确定字符串是否实际上是 GUID 的字符串表示形式:

  1. 正则表达式
  2. 检查字符串中的所有字符
  3. 使用给定字符串创建新的 Guid 实例(如果可以创建 Guid,那么该字符串是有效的字符串表示形式)

这是代码:

[测试]

public void Test_IsRegex_Performance()
{
    Action withRegexMatch = () =>
    {
        Regex regex = new Regex("^[0-9a-f]{32}$");
        Guid g = new Guid();
        string s = g.ToString();
        regex.IsMatch(s);
    };

    Action withCharCheck = () =>
    {
        Guid g = new Guid();
        string s = g.ToString();

        Func<char, bool> charValid = c => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
        bool isValid = s.Length == 32 && s.All(charValid);
    };

    Action withNewGuid = () =>
    {
        Guid g = new Guid();
        string s = g.ToString();
        try
        {
            Guid g2 = new Guid(s);
            // if no exception is thrown, this is a valid string 
            // representation
        }
        catch
        {
            // if an exception was thrown, this is an invalid
            // string representation
        }
    };

    const int times = 100000;

    Console.WriteLine("Regex: {0}", TimedTask(withRegexMatch, times));
    Console.WriteLine("Checking chars: {0}", TimedTask(withCharCheck, times));
    Console.WriteLine("New Guid: {0}", TimedTask(withNewGuid, times));
    Assert.Fail();
}

private static TimeSpan TimedTask(Action action, int times)
{
    Stopwatch timer = new Stopwatch();
    timer.Start();
    for (int i = 0; i < times; i++)
    {
        action();
    }
    timer.Stop();
    return timer.Elapsed;
}

在我的机器上进行一百万次迭代的结果:

Regex: 00:00:10.1786901
Checking chars: 00:00:00.2504520
New Guid: 00:00:01.3129005

因此,正则表达式解决方案很慢。问问自己是否真的需要一个正则表达式。请注意,您可能可以通过仅声明一次正则表达式并重用它来获得一些额外的性能,但我认为这种情况下的要点是,通过查看您正在做什么,您可能会取得更好的成功尝试完成,而不是如何

希望有帮助。

Ok, so there's an answer here that shows you how to find out if this string is 32 characters in length, and each character is either a digit or lowercase 'a' to lowercase 'f' with a regex, and another that shows you how to do it with a simple Func which scans that characters in the string. They're both great answers and technically correct, but in your question title you explicitly state "GUID" which opens up a new can of worms.

GUIDs can take a number of different string representations, and you might encounter any one of them. Do you need to handle all of these? Will you need to accomodate strings that begin and end with curly braces ('{' and '}') or parentheses? What about dashes ('-')? According to MSDN, creating a new GUID with

string s = ...;
Guid g = new Guid(s);

Allows for strings in the following forms

32 contiguous digits:
dddddddddddddddddddddddddddddddd

-or-

Groups of 8, 4, 4, 4, and 12 digits
with hyphens between the groups. The
entire GUID can optionally be enclosed
in matching braces or parentheses:
dddddddd-dddd-dddd-dddd-dddddddddddd

-or-

{dddddddd-dddd-dddd-dddd-dddddddddddd}

-or-

(dddddddd-dddd-dddd-dddd-dddddddddddd)

-or-

Groups of 8, 4, and 4 digits, and
a subset of eight groups of 2 digits,
with each group prefixed by "0x" or
"0X", and separated by commas. The
entire GUID, as well as the subset, is
enclosed in matching braces: {0xdddddddd, 0xdddd, 0xdddd,{0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd,0xdd}}

Do you need to handle all of these cases? Also, consider if using a regex is really the best option. As some people have already commented, regex can be confusing for some developers, and the intent isn't always clear. On top of that, regex can be slow in some cases.

I whipped up a quick performance test on three different ways of determining if a string is in fact a string representation of a GUID:

  1. Regex
  2. Checking all chars in the string
  3. Creating a new Guid instance with the given string (if the Guid can be created, then the string is a valid string representation)

Here's the code:

[Test]

public void Test_IsRegex_Performance()
{
    Action withRegexMatch = () =>
    {
        Regex regex = new Regex("^[0-9a-f]{32}$");
        Guid g = new Guid();
        string s = g.ToString();
        regex.IsMatch(s);
    };

    Action withCharCheck = () =>
    {
        Guid g = new Guid();
        string s = g.ToString();

        Func<char, bool> charValid = c => (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f');
        bool isValid = s.Length == 32 && s.All(charValid);
    };

    Action withNewGuid = () =>
    {
        Guid g = new Guid();
        string s = g.ToString();
        try
        {
            Guid g2 = new Guid(s);
            // if no exception is thrown, this is a valid string 
            // representation
        }
        catch
        {
            // if an exception was thrown, this is an invalid
            // string representation
        }
    };

    const int times = 100000;

    Console.WriteLine("Regex: {0}", TimedTask(withRegexMatch, times));
    Console.WriteLine("Checking chars: {0}", TimedTask(withCharCheck, times));
    Console.WriteLine("New Guid: {0}", TimedTask(withNewGuid, times));
    Assert.Fail();
}

private static TimeSpan TimedTask(Action action, int times)
{
    Stopwatch timer = new Stopwatch();
    timer.Start();
    for (int i = 0; i < times; i++)
    {
        action();
    }
    timer.Stop();
    return timer.Elapsed;
}

And the results from a million iterations on my machine:

Regex: 00:00:10.1786901
Checking chars: 00:00:00.2504520
New Guid: 00:00:01.3129005

So, the regex solution is slow. Ask yourself if you really need a regex here. Note that you can probably eek out some extra performance by only declaring the regex once, and reusing it, but I think the point in this case is that you might have better success by looking at what you're trying to accomplish, as opposed to how.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文