正则表达式最好的书籍

C#概念(C# CONCEPTS)

In most of the cases, RegEx engine performs pattern matching quickly and efficiently. However, in some cases, the engine may seem very lazy.

在大多数情况下,RegEx引擎可以快速有效地执行模式匹配。 但是,在某些情况下,引擎可能看起来很懒。

The article describes some of the best practices that developers can adopt to ensure that regular expressions produce optimal performance.

本文介绍了开发人员可以用来确保正则表达式产生最佳性能的一些最佳实践。

5种不同用例的正则表达式简介: (Intro to Regular Expression with 5 different use cases:)

考虑输入源 (Consider Input Source)

Regular Expressions accepts two types of inputs:

正则表达式接受两种类型的输入:

  • Constrained: When inputting text originates from a known source.

    受限:输入文本时,其来源是已知的。

  • Unconstrained: When inputting text originates from an unreliable source.

    不受限制:输入文本时,来源不可靠。

To test unconstrained input, a RegEx must handle:

要测试不受约束的输入,RegEx必须处理:

  • Text that successfully matches the RegEx pattern.成功匹配RegEx模式的文本。
  • Text that does not match the RegEx pattern.与RegEx模式不匹配的文本。
  • Text that nearly matches the RegEx pattern.与RegEx模式几乎匹配的文本。

Consider an example of text string match for RegEx @”^[0–9A-Z]([-.\w]*[0–9A-Z])*$” and check constrained and unconstrained input performance.

考虑RegEx @”^[0–9A-Z]([-.\w]*[0–9A-Z])*$”的文本字符串匹配示例,并检查约束和不受约束的输入性能。

static void InputConsiderExample()
{Stopwatch sw;string inputValue = "AAAAAAAAAAA"; // Constrained Input//string inputValue="aaaaaAAAAAAAA!"; // UnConstrained Inputstring pattern = @"^[0-9A-Z]([-.\w]*[0-9A-Z])*$";string input;int index = 0;for (int ctr = inputValue.Length - 1; ctr >= 0; ctr--){index++;input = inputValue.Substring(ctr, index);sw = Stopwatch.StartNew();Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase);sw.Stop();if (m.Success)Console.WriteLine("{0,2}. Matched '{1,25}' in {2}",index, m.Value, sw.Elapsed);elseConsole.WriteLine("{0,2}. Failed '{1,25}' in {2}",index, input, sw.Elapsed);         }}

代码说明(Code Description)

The above code matches each character of the input string with RegEx pattern and output success/failure along with time elapsed.

上面的代码将输入字符串的每个字符与RegEx模式匹配,输出成功/失败以及经过的时间。

测试约束输入 (Test for constrained input)

inputValue="AAAAAAAAAAA"Output====== 1. Matched '                        A' in 00:00:00.0143831 2. Matched '                       AA' in 00:00:00.0001389 3. Matched '                      AAA' in 00:00:00.0000069 4. Matched '                     AAAA' in 00:00:00.0000057 5. Matched '                    AAAAA' in 00:00:00.0000194 6. Matched '                   AAAAAA' in 00:00:00.0000155 7. Matched '                  AAAAAAA' in 00:00:00.0000118 8. Matched '                 AAAAAAAA' in 00:00:00.0000223 9. Matched '                AAAAAAAAA' in 00:00:00.000023710. Matched '               AAAAAAAAAA' in 00:00:00.000011111. Matched '              AAAAAAAAAAA' in 00:00:00.0000111

As we can notice, the time elapsed is similar concerning the length of the input.

我们可以注意到,关于输入的长度,经过的时间是相似的。

测试无限制的输入 (Test for unconstrained input)

inputValue="aaaaaAAAAAAAA!"Output====== 1. Failed '              !' in 00:00:00.0128292 2. Failed '             A!' in 00:00:00.0001336 3. Failed '            AA!' in 00:00:00.0000150 4. Failed '           AAA!' in 00:00:00.0000073 5. Failed '          AAAA!' in 00:00:00.0000090 6. Failed '         AAAAA!' in 00:00:00.0000146 7. Failed '        AAAAAA!' in 00:00:00.0000380 8. Failed '       AAAAAAA!' in 00:00:00.0000239 9. Failed '      AAAAAAAA!' in 00:00:00.000058710. Failed '     aAAAAAAAA!' in 00:00:00.000191411. Failed '    aaAAAAAAAA!' in 00:00:00.000357412. Failed '   aaaAAAAAAAA!' in 00:00:00.000737113. Failed '  aaaaAAAAAAAA!' in 00:00:00.001101014. Failed ' aaaaaAAAAAAAA!' in 00:00:00.0018678

As we can notice, the time elapsed almost doubled with an increase in the length of the unconstrained input.

我们可以注意到,随着无约束输入的长度增加,经过的时间几乎翻了一番。

解决这个问题。 (To solve this problem.)

  • When developing a RegEx, you should consider how Backtracking might affect the performance of the regular expression engine, mainly if your regular expression is designed to process unconstrained input.开发RegEx时,应考虑回溯如何影响正则表达式引擎的性能,主要是在正则表达式设计为处理不受约束的输入的情况下。
  • Thoroughly test your RegEx utilising invalid and near-valid data as well as valid information.使用无效和接近有效的数据以及有效信息来彻底测试RegEx。

适当选择实例化 (Choose instantiation appropriately)

There are four different ways to couple RegEx with a particular pattern to RegEx Engine.

有四种将RegEx与特定模式耦合到RegEx Engine的方法。

使用静态方法 (Use static Method)

The static method does not require any instantiation. Method name is Regex.Match(String, String)

静态方法不需要任何实例化。 方法名称是Regex.Match(String, String)

Consider an example of RegEx to match a valid currency.

考虑一个正则表达式示例来匹配有效货币。

Below example creates a RegEx object each time — INEFFICIENT

下面的示例每次都会创建一个RegEx对象-INEFFICIENT

string pattern = @"\p{Sc}+\s*\d+";       Regex currencyRegex = new Regex(pattern);       return currencyRegex.IsMatch(currencyValue);

Replace the inefficient way with a call to the static Regex.Match(String, String) the method as shown below

用对静态Regex.Match(String, String)的调用代替低效的方法,如下所示

string pattern = @"\p{Sc}+\s*\d+";       return Regex.IsMatch(currencyValue, pattern);

Pattern Breakdown: @”\p{Sc}+\s*\d+”

模式分类: @”\p{Sc}+\s*\d+”

使用不带选项标志的RegEx实例化 (Use RegEx instantiation without options flag)

Regex object is instantiated without an options argument that includes the Compiled flag.

实例化正则表达式对象,而没有包含Compiled标志的options argument

使用带有选项标志的RegEx实例化。 (Use RegEx instantiation with options flag.)

Regex object is instantiated with an options argument that includes the Compiled flag.

正则表达式对象使用一个包含Compiled标志的options argument实例化。

There are two types of options available

有两种类型的选项

  • Interpreted RegEx解释正则表达式
  • Compiled RegEx编译正则表达式

Specify Interpreted RegEx

指定解释的正则表达式

new Regex(pattern, RegexOptions.Singleline)

Specify Compiled RegEx

指定编译的正则表达式

new Regex(pattern, RegexOptions.Compiled)

使用CompileToAssembly方法 (Use the CompileToAssembly MethodMethod)

Use this MethodMethod when the RegEx object is tightly coupled with a regular expression and save it to assembly using Regex.CompileToAssembly method.

当RegEx对象与正则表达式紧密耦合时,请使用此MethodMethod,并使用Regex.CompileToAssembly方法将其保存到程序Regex.CompileToAssembly

考虑回溯 (Consider Backtracking)

When quantifiers such as *, +, and ? are used inside a RegEx, then the engine may give up a portion of partial matches to a previous success state to match faster for the complete pattern. This process is called Backtracking.

*+?等量词在正则表达式中使用“否”,则引擎可能会放弃对先前成功状态的部分匹配的一部分,以针对完整模式更快地进行匹配。 此过程称为回溯

Although backtracking showcase RegEx power & flexibility but excessive use of it also degrades performance.

尽管回溯展示了RegEx的功能和灵活性,但过度使用它也会降低性能。

Consider an example of Regex which gets all words that start with a capital letter. Let’s understand backtracking with that regular expression.

考虑一个正则表达式的示例,该示例获取所有以大写字母开头的单词。 让我们了解使用该正则表达式的回溯。

有回溯 (With Backtracking)

string input = "Hello my Name is Sukhpinder";
string pattern = @"\b\p{Lu}\w*\b";foreach (Match match in Regex.Matches(input, pattern))Console.WriteLine(match.Value);// Output
// Hello
// Name
// Sukhpinder

Pattern Breakdown: @”\b\p{Lu}\w*\b”

模式分类: @”\b\p{Lu}\w*\b”

禁用回溯 (Disable Backtracking)

To disable it use the(?>subexpression) language element, known as an atomic group.

要禁用它,请使用(?>subexpression)语言元素,称为原子组。

没有回溯:请注意模式更改 (Without Backtracking: Notice the pattern change)

string input = "Hello my Name is Sukhpinder";
string pattern = @"\b\p{Lu}(?>\w*)\b";foreach (Match match in Regex.Matches(input, pattern))Console.WriteLine(match.Value);// Output
// Hello
// Name
// Sukhpinder

使用超时超载(Use TimeOut Overloads)

If your regular expressions process input that nearly matches the RegEx pattern, it relies on Backtracking, which in turn impacts the performance.

如果您的正则表达式处理的输入几乎与RegEx模式匹配,则它依赖于Backtracking,这反过来会影响性能。

Always set a time-out condition to lower the influence of excessive Backtracking.

始终设置超时条件以降低过度回溯的影响。

Overloads- Regex(String, RegexOptions, TimeSpan)- Regex.Match(String, String, RegexOptions, TimeSpan)

更多C#概念 (More C# Concepts)

Thank you for reading. I hope you like the article..!!

感谢您的阅读。 我希望你喜欢这篇文章。

翻译自: https://medium.com/swlh/best-practices-for-regular-expressions-b39521ccacbf

正则表达式最好的书籍


http://www.taodudu.cc/news/show-2301275.html

相关文章:

  • 正则表达式使用汇总
  • 正则应用之——日期正则表达式
  • python正则匹配日期2019-03-11_正则表达式验证日期(多种日期格式)——转载
  • 正则表达式科学计数法_数据科学家的正则表达式
  • 正则表达式(以校验qq是否合法等为例)
  • 将网页内容保存为PDF
  • 扩展工具将网页保存为PDF
  • 怎么把网页保存成pdf格式-最简单操作
  • python 爬取文章并保存为pdf
  • 保存网页内容为PDF,支持文本复制,链接跳转
  • 将CSDN博客保存为PDF
  • 网页保存为pdf
  • 牛逼!40行Python代码一键把html网页保存为pdf,太方便了!
  • 电商运营学习成长目录
  • 电商学习目录
  • 电商秒杀学习之路
  • Vue学习--黑马电商管理系统
  • python对电商运营有帮助吗_做电商运营需要学习python嘛?
  • GitHub开源项目学习 电商系统Mall (一) Mall简介
  • 深度学习实战7-电商产品评论的情感分析
  • 【跨境电商学习指南】
  • 学习RPA-电商自动化入门①
  • 【20220912】电商业务的核心流程
  • 电商数据分析常用指标及意义
  • python学强化学习
  • 【博学谷学习记录】超强总结,用心分享 | 产品经理电商项目知识点总结与回顾
  • python电商项目介绍_django打造电商项目
  • 机器学习实战 | 综合项目-电商销量预估进阶方案
  • 机器学习在电商应用中的三个境界:爆款模型、转化率模型及个性化模型
  • 电商架构讲解

正则表达式最好的书籍_正则表达式的最佳做法相关推荐

  1. java正则表达式 文件后缀名_正则表达式 判断文件名后缀是否为 csv xls xlsx

    展开全部 在input中想要获取32313133353236313431303231363533e58685e5aeb931333365646262上传的文件名,那么onclick事件是不行的,因为一 ...

  2. 正则表达式只能写数字_正则表达式真的很骚,可惜你不会写

    源:https://juejin.im/post/5b96a8e2e51d450e6a2de115 本文旨在用最通俗的语言讲述最枯燥的基本知识 文章提纲: 元字符 重复限定符 分组 转义 条件或 区间 ...

  3. mysql正则表达式中括号单汉字_正则表达式中(括号) [方括号] {大括号}的区别

    之前遇到要用正则判断输入字符串的小需求中遇到了问题,连续的字符串不知道怎么判断. 如邮箱结尾的.com .cn .net等. 于是乎查阅资料: 括号() : 括号是多个匹配,它把括号内的当做一组来处理 ...

  4. python正则表达式取中间字符串_正则表达式如何获取中间字符串

    如果问题真的是"这个表达有什么问题?",答案如下:您试图将换行符与.*?匹配.您需要(?s)才能使其工作.在 在正则表达式的中间有显式的空格和换行符,它们在源文本中没有任何对应的字 ...

  5. python正则表达式匹配中文标点符号_正则表达式-匹配标点符号

    string testStr = "你好,测试."; Regex reg= new Regex(@"\p{P}"); testStr= reg.Replace( ...

  6. java正则表达式去除括号及_正则表达式去除中括号(符号)及里面包含的内容...

    例子:颜色:粉色[10] 尺码:S[5] 去掉[ ]及内容: preg_replace("/\[.*\]/", '', $str)1 处理后效果:颜色:粉色 尺码:S 小技巧:可把 ...

  7. java 正则表达式 非贪婪模式_正则表达式的贪婪模式和非贪婪模式

    一.什么是贪婪模式和非贪婪模式? 定义 贪婪模式:正则表达式趋向于匹配最大长度. 非贪婪模式:正则表达式趋向于匹配最小长度,即一旦匹配到结果就结束. 如何区分? 默认是贪婪模式. 若 量词 后面添加 ...

  8. dita最佳实践指南_艺术资产–最佳做法指南

    dita最佳实践指南 Unity支持来自各种程序或来源的纹理3D模型. 该简短指南由游戏美术师和Unity开发人员共同整理,以帮助您创建在Unity项目中可以更好,更高效地工作的资产. 这将在适当时候 ...

  9. php 正则表达式 文档,php的正则表达式完全手册

    php的正则表达式完全手册 更新时间:2011年02月06日 17:12:49   作者: 正则表达式是烦琐的,但是强大的,学会之后的应用会让你除了提高效率外,会给你带来绝对的成就感.只要认真去阅读这 ...

  10. python正则表达式入门_python入门_常见正则表达式匹配

    made_in_miliLV的主页.png - 正则表达式:正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配.在开发的使用场景 - 例如:匹配url等等 本文中,对正则 ...

最新文章

  1. Linux系统文件以及目录介绍
  2. HCNE题库附件、可下载
  3. secureCRT自动断开的解决方法
  4. 谭浩强课后题之----求最大公约数和最小公倍数
  5. linux常用命令:sudo 命令
  6. shell脚本一键同时推送代码至github和gitee
  7. MySQL 批量插入数据,单次插入多少条数据效率最高
  8. linux批量替换文件名中的相同字符
  9. memcached内存管理及key value长度限制
  10. Python生成1000个随机字符的字符串,统计每个字符的出现次数(choices函数和Counter的使用)
  11. C# AE axGlobeControl The 3D Analyst extension has not been enabled.
  12. ADSL接入路由,电脑共享上网
  13. Web静态服务器-7-gevent版
  14. freeswitch的dialplan中condition变量
  15. Foobar2000的配置及优化
  16. c语言常量指什么作用,c语言字符常量是什么
  17. 青岛大学计算机科学技术学院辛立强,赵志刚-青岛大学计算机科学技术学院
  18. 微服务 弹性伸缩_如何构建弹性微服务
  19. 使用markdownpad生成目录
  20. R语言数据可视化包ggplot2画图之散点图

热门文章

  1. Semaphore释疑
  2. 移动侦测/周界入侵检测智能分析摄像头采集的智能分析数据如何通过国标GB28181平台进行接入与分发
  3. PHP Encoder (ioncube 10) 加密工具的使用
  4. PHP家庭账单系统,php在线家庭财务管理系统
  5. python 广义线性模型_scikit-learn 1.1 广义线性模型(Generalized Linear Models)
  6. 第4章 Function语义学
  7. Android 常用的语言代码与国家地区对照表
  8. Photoshop CS6 破解安装
  9. 关于华为pad(鸿蒙系统)连接wedo 2.0 集线器问题
  10. 利用openFrameWork、ofxFaceTrack制作的虚拟眼镜加载系统