.jpg)
我昨天对有人在正则表达式中使用而不是 or 的答案发表了评论。我说使用范围或数字说明符可能比字符集更有效。[0123456789][0-9]d
我决定今天进行测试,并惊讶地发现(至少在 c# 正则表达式引擎中)似乎比其他两个似乎没有太大区别的任何一个效率低。这是我的测试输出超过 10000 个随机字符串,其中包含 1000 个随机字符,其中 5077 个实际包含一个数字:d
Regex d took 00:00:00.2141226 result: 5077/10000
Regex [0-9] took 00:00:00.1357972 result: 5077/10000 63.42 % of first
Regex [0123456789] took 00:00:00.1388997 result: 5077/10000 64.87 % of first
这对我来说是一个惊喜,原因有两个,如果有人能阐明一些信息,我会很感兴趣:
d[0-9]d[0-9]以下是测试代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace SO_RegexPerformance
{
class Program
{
static void Main(string[] args)
{
var rand = new Random(1234);
var strings = new List<string>();
//10K random strings
for (var i = 0; i < 10000; i++)
{
//generate random string
var sb = new StringBuilder();
for (var c = 0; c < 1000; c++)
{
//add a-z randomly
sb.Append((char)('a' + rand.Next(26)));
}
//in roughly 50% of them, put a digit
if (rand.Next(2) == 0)
{
//replace 1 char with a digit 0-9
sb[rand.Next(sb.Length)] = (char)('0' + rand.Next(10));
}
strings.Add(sb.ToString());
}
var baseTime = testPerfomance(strings, @"d");
Console.WriteLine();
var testTime = testPerfomance(strings, "[0-9]");
Console.WriteLine(" {0:P2} of first", testTime.TotalMilliseconds / baseTime.TotalMilliseconds);
testTime = testPerfomance(strings, "[0123456789]");
Console.WriteLine(" {0:P2} of first", testTime.TotalMilliseconds / baseTime.TotalMilliseconds);
}
private static TimeSpan testPerfomance(List<string> strings, string regex)
{
var sw = new Stopwatch();
int successes = 0;
var rex = new Regex(regex);
sw.Start();
foreach (var str in strings)
{
if (rex.Match(str).Success)
{
successes++;
}
}
sw.Stop();
Console.Write("Regex {0,-12} took {1} result: {2}/{3}", regex, sw.Elapsed, successes, strings.Count);
return sw.Elapsed;
}
}
}

网友回答:
感谢ByteBlast在文档中注意到这一点。只需更改正则表达式构造函数:
var rex = new Regex(regex, RegexOptions.ECMAScript);
提供新的时间:
Regex d took 00:00:00.1355787 result: 5077/10000
Regex [0-9] took 00:00:00.1360403 result: 5077/10000 100.34 % of first
Regex [0123456789] took 00:00:00.1362112 result: 5077/10000 100.47 % of first

网友回答:
d检查所有 Unicode 数字,但仅限于这 10 个字符。例如,波斯数字 是 Unicode 数字的一个示例,它与 匹配,但不匹配。[0-9]۱۲۳۴۵۶۷۸۹d[0-9]
您可以使用以下代码生成所有此类字符的列表:
var sb = new StringBuilder();
for(UInt16 i = 0; i < UInt16.MaxValue; i++)
{
string str = Convert.ToChar(i).ToString();
if (Regex.IsMatch(str, @"d"))
sb.Append(str);
}
Console.WriteLine(sb.ToString());
这将产生:
0123456789٠١٢٣٤٥٦٧٨٩۰۱۲۳۴۵۶۷۸۹إئئॶ૦ئ२३४५६७८९০১২৩৪৫৬৭৮৯੦੧੨੩੪੫੬੭੮੯૦૧૨૩૪૦૬૭૮૯୦୧୨୩୪୫୬୭୭�୭�୭୭�୭୭୭��୯௦௧௨௩௪௫௬௭௮௯౦౧౨౩౪౫౬౭౮౯೦೧೨೩೪೫೬೭೮೯൦൧൨൩൪൫൬൭൮൯๐๑๒๓๔๕๖๗๘๙໐໑໒໓໔໕໖໗໘໙༠༡༢༣༤༥༦༧༨༩Ἡṗ๘๐໑໗໘໙༠༡༢༣༤༥༦༧༨༩Ἡṗ๘໐໑໓໔໕໖໗໘༙༠༡༢༣༤༥༦༧༨༩ἩṒ๓๔๕๖๗๘໐໑໗໓໔໕໖໗໘༙༠༡༢༣༤༥༦༧༨༩ἩṒ๓๔๕๖๗๘໐໑໓໔໕໖໗໘໙༠༡༢༣༤༥༦༧༨༩ṩṗ๘๙໐໑໓໔໕໖໗໘໙༠༡༢༣༤༥༦༧༨༩ṩṒ๓๔๕๖๗๘๙໐໑໓໔໕໖໗໘༙༰་༡༢༣༤༥༦�偀၁၂၃၄၅၆၇၈၉႐႑႒႓႔႕႖႗႘႙០១២៣៤៥៦៧៨៩᠐᠑᠒᠓᠔᠕᠖᠗᠘᠙᥆᥇᥈᥉᥊᥋᥍᥎᥏᧐᧑᧒᧓ᧃ᧕᧖᧖᧓ᭃ᭖᭗᭘᭙᮰᮱᮱ ��᮳᮴᮵᮶᮷᮸᮹Ṁ᱁Ṃ᱃᱄᱅᱆᱇᱈᱉᱐᱑᱒᱓᱔᱕᱖᱗᱘᱙꘠꘡꘢꘣꘤꘥꘦꘧꘨꘩ꘐ꣑꣒꣓꣔꣕꣖꣗꣘꣙꤀꤁꤂꤃꤄꤅꤆꤇꤈꤉꩐꩑꩒꩓ꩩ꩕꩖꩗꩘꩙0123456789

网友回答:
从正则表达式中的“d”是否表示数字?
[0-9]不等效于 。 仅匹配字符,而匹配和其他数字字符,例如东方阿拉伯数字d[0-9]0123456789d[0-9]٠١٢٣٤٥٦٧٨٩
模板简介:该模板名称为【使用范围或数字说明符可能比字符集更有效】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【C#】栏目查找您需要的精美模板。