如何编写更易于维护的正则表达式? [英] How do I write more maintainable regular expressions?

查看:45
本文介绍了如何编写更易于维护的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始觉得使用正则表达式会降低代码的可维护性.正则表达式的简洁性和强大功能有些邪恶.Perl 将其与默认运算符等副作用结合在一起.

I have started to feel that using regular expressions decreases code maintainability. There is something evil about the terseness and power of regular expressions. Perl compounds this with side effects like default operators.

我确实习惯于用至少一个句子给出基本意图和至少一个匹配的例子来记录正则表达式.

I DO have a habit of documenting regular expressions with at least one sentence giving the basic intent and at least one example of what would match.

因为正则表达式是建立起来的,所以我觉得绝对有必要对表达式中每个元素的最大组件进行评论.尽管如此,即使是我自己的正则表达式也让我摸不着头脑,就好像我在阅读克林贡语一样.

Because regular expressions are built up I feel it is an absolute necessity to comment on the largest components of each element in the expression. Despite this even my own regular expressions have me scratching my head as though I am reading Klingon.

你是故意把你的正则表达式变笨吗?你是否将可能更短、更强大的分解成更简单的步骤?我已经放弃了嵌套正则表达式.是否存在由于可维护性问题而避免使用的正则表达式结构?

Do you intentionally dumb down your regular expressions? Do you decompose possibly shorter and more powerful ones into simpler steps? I have given up on nesting regular expressions. Are there regular expression constructs that you avoid due to mainainability issues?

不要让这个例子掩盖问题.

如果 Michael Ash 的以下内容存在某种错误,您会发现除了完全扔掉它之外,还有什么可能做任何事情吗?

If the following by Michael Ash had some sort of bug in it would you have any prospects of doing anything but throwing it away entirely?

^(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[13-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$

根据请求,可以使用上面 Ash 先生的链接找到确切的用途.

Per request the exact purpose can be found using Mr. Ash's link above.

匹配 01.1.02 |11-30-2001 |2/29/2000

Matches 01.1.02 | 11-30-2001 | 2/29/2000

不匹配 02/29/01 |13/01/2002 |11/00/02

Non-Matches 02/29/01 | 13/01/2002 | 11/00/02

推荐答案

我通常只是尝试将我所有的正则表达式调用包装在它们自己的函数中,使用一个有意义的名称和一些基本的注释.我喜欢将正则表达式视为一种只写语言,只有编写它的人才能阅读(除非它真的很简单).我完全希望有人可能需要完全重写表达式,如果他们不得不改变其意图,这可能是为了更好地保持正则表达式训练的活力.

I usually just try to wrap all my Regular Expression calls inside their own function, with a meaningful name and an some basic comments. I like to think of Regular Expressions as a write only language, readable only by the one that wrote it (Unless it's really simple). I fully expect that someone would need to probably completely re-write the expression if they had to change its intent and this is probably for the better to keep the Regular Expression training alive.

这篇关于如何编写更易于维护的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆