pattern matching - Composed Regular Expressions - breaking a regex down into a readable form -


I was reading by Martin Fowler putting an article, this is where you can take codes like this:

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

and break it in something else:

  protected override string GetPattern () {const string pattern = @ "^ score \ s + ( \ D +) # digits \ s + for \ s + (\ d +) # number of nights + nights? #optional plural \ s + at + (. *) # Hotel name "; Return pattern; }}  

Or this:

  const string scorekeyword = @ "^ score \ s +"; Conf. String number of points = @ "(\ d +)"; Const string for the keyword = @ "\ s + for \ s +"; Const String NumberOffnets = @ "(\ d +)"; Const String Nights AtWord = @ "\ s + Nights? \ S + At \ s +"; Const String Hotelame = @ "(. *)"; Constant String Pattern = ScoreQuever + Number of Points + Career + Numberoffnights + NightsnightKeyword + Hostname;  

Or even:

  const string space = @ "\ s +"; Const String Start = "^"; Conf. String number of points = @ "(\ d +)"; Const String NumberOffnets = @ "(\ d +)"; Const string nights at wordpress = @ "nights? \ S + A"; Const String Hotelame = @ "(. *)"; Const String Pattern = Start + "Score" + Space + numberoff points + space + "for" + space + numberof + space + nightsetkeyword + space + hostname;  

It seems easy enough to do this and it can have some benefits. I'm not a "regex man" in any way and often find myself finding myself to take the time to write it for myself instead of finding the regex that I found to find a net problem For just the right regex I copy and paste it and test it to ensure that it advertises. Then I can leave a comment that describes what it does. But then I am facing big and better things in life.

I am thinking that breaking a Rijks pattern in the manner described in the Martin Faulter's 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, right now it is in many pieces if you ever need to extend that expression ... it really helps you understand that what rijks do Has been doing?

I know that all die hard Pearl people love their Reggae patterns, but for those who with a regex more than every other project ( Question ) Do not deal, do you break the regex pattern and can be read more or less? Has anyone employed this concept in a project and found it useful? ... not useful?

Yes, of course. Reggae is powerful, but due to his brief sentence, it is extremely negligible when I read a comment, like "it matches a URI", which does not really help me to understand how how It does, and should show me where (for example) the query string does not exactly match the case of some vague corners. Reggae Code; As document, if you had a function document, if it is small and (proper) is clear, a single comment for whole regex is OK. If it's complicated, clearly highlight and comment on different parts. If this is really complex, then divide it into several regdets.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -