组合正则表达式 - 将正则表达式分解为可读形式 [英] Composed Regular Expressions - breaking a regex down into a readable form

查看:60
本文介绍了组合正则表达式 - 将正则表达式分解为可读形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Martin Fowler 撰写的一篇关于组合正则表达式的文章.您可以在此处使用如下代码:

I was reading an article put together by Martin Fowler regarding Composed Regular Expressions. This is where you might take code such as this:

const string pattern = @"^score\s+(\d+)\s+for\s+(\d+)\s+nights?\s+at\s+(.*)";

把它分解成更像这样的东西:

And break it out into something more like this:

protected override string GetPattern() {
      const string pattern =
        @"^score
        \s+  
        (\d+)          # points
        \s+
        for
        \s+
        (\d+)          # number of nights
        \s+
        night
        s?             #optional plural
        \s+
        at
        \s+
        (.*)           # hotel name
        ";

      return pattern;
    }
  }

或者这个:

const string scoreKeyword = @"^score\s+";
const string numberOfPoints = @"(\d+)";
const string forKeyword = @"\s+for\s+";
const string numberOfNights = @"(\d+)";
const string nightsAtKeyword = @"\s+nights?\s+at\s+";
const string hotelName = @"(.*)";

const string pattern =  scoreKeyword + numberOfPoints +
  forKeyword + numberOfNights + nightsAtKeyword + hotelName;

甚至这个:

const string space = @"\s+";
const string start = "^";
const string numberOfPoints = @"(\d+)";
const string numberOfNights = @"(\d+)";
const string nightsAtKeyword = @"nights?\s+at";
const string hotelName = @"(.*)";

const string pattern =  start + "score" + space + numberOfPoints + space +
  "for" + space + numberOfNights + space + nightsAtKeyword + 
   space + hotelName;

这听起来很容易做到,而且可能有一些好处.我绝不是一个正则表达式人",经常发现自己在网上搜索以找到我需要的正则表达式,而不是花时间为自己编写它.找到适合给定问题的正则表达式后,我将其复制并粘贴到其中并进行测试,以确保其符合广告宣传的要求.然后我可能会发表评论来描述它的作用.但后来我开始了生活中更大更好的事情.

This sounds easy enough to do and might have some benefits to it. I am by no means a "regex guy" and frequently find myself searching the net to locate the regex that I need rather than taking the time to compose it for myself. Having found that just right regex for a given problem I copy and paste it in and test it to make sure it does as advertised. I then might drop a comment describing what it does. But then I am off to bigger and better things in life.

我想知道以 Martin Fowlers 讨论中描述的方式打破正则表达式模式实际上是否比评论更容易理解?在一天结束时,您的代码中仍然有一个丑陋的正则表达式,只是现在它分成了很多部分.如果您需要扩展该表达式...这如何帮助您真正理解正则表达式的作用?

I am wondering if breaking a regex pattern up in the way described in Martin Fowlers discussion will actually make things easier to understand than a comment? At the end of the day you still have an ugly regex in your code only now it is in many pieces. If ever you need to extend that expression...how does this help you really understand what the regex is doing?

我知道所有顽固的 perl 家伙都喜欢他们的正则表达式模式.但是对于那些不每隔一个项目多次处理正则表达式的人(问题),您是否发现将正则表达式模式分成更小的位或多或少可读?有没有人在项目中使用过这个概念并发现它有用?……没用?

I know all the die-hard perl guys out there love their regex patterns. But for those that don't deal with a regex more than once every other project (question) do you find the breaking of the regex pattern into smaller bits more or less readable? Has anyone employed this concept in a project and found it useful? ...not useful?

推荐答案

是的,绝对的.正则表达式功能强大,但由于其简洁的语法,极不可读.当我阅读诸如这匹配一个 URI"之类的评论时,这实际上并没有帮助我弄清楚它是如何做到的,以及我应该在哪里寻找(例如)修复它的错误没有正确匹配查询字符串中的一些晦涩的极端情况.正则表达式是代码;像记录函数一样记录它.如果它简短且(合理)清晰,则整个正则表达式的单个注释就可以了.如果它很复杂,请清楚地突出显示和评论各个部分.如果它真的很复杂,请将其拆分为多个正则表达式.

Yes, absolutely. Regexes are powerful, but because of their terse syntax, extremely unreadable. When I read a comment such as "this matches an URI", that doesn't actually help me figure out how it does that, and where I should look to (for example) fix a bug where it doesn't match some obscure corner case in query string properly. Regex is code; document it as you'd document a function. If it's short and (reasonably) clear, a single comment for the entire regex is fine. If it's complicated, clearly highlight and comment individual parts. If it's really complex, split it into several regexes.

这篇关于组合正则表达式 - 将正则表达式分解为可读形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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