Skip to main content
Post Merged (destination) from programmers.stackexchange.com/questions/10998/…
added 521 characters in body
Source Link
Peter Boughton
  • 4.6k
  • 1
  • 32
  • 27

RegularExpressionsarenoworsetoreadormaintainthananyotherunformattedcontent;indeedaregexisprobablyeasiertoreadthatthispiecehereRegularExpressionsarenoworsetoreadormaintainthananyotherunformattedcontent;indeedaregexisprobablyeasiertoreadthanthispieceoftexthere-butunfortunatelytheyhaveabadreputationbecausesomeimplementationsdon'tallowformattingandpeopleingeneraldon'tknowthatyoucandoit.

(Regular Expressions are no worse to read or maintain than any other unformatted content; indeed a regex is probably easier to read than this piece of text here - but unfortunately they have a bad reputation because some implementations don't allow formatting and people in general don't know that you can do it.)


Here's a trivial example:
^(?:[^,]*+,){21}[^,]*+$ 

Which isn't really that difficult to read or maintain anyway, but is even easier when it looks like this:
(?x) # enables comments, so this whole block can be used in a regex. ^ # start of string (?: # start non-capturing group [^,]*+ # as many non-commas as possible, but none required , # a comma ) # end non-capturing group {21} # 21 of previous entity (i.e. the group) [^,]*+ # as many non-commas as possible, but none required $ # end of string 

That's a bit of an over-the-top example (commenting $ is akin to commenting i++) but clearly there should be no problem reading and, understanding that, and if maintenance is required it should be fine toomaintaining that.


So long as you're clear as to when regular expressions are suited and when they're a bad idea, there's nothing wrong with them, and most times the JWZ quote doesn't really apply.

RegularExpressionsarenoworsetoreadormaintainthananyotherunformattedcontent;indeedaregexisprobablyeasiertoreadthatthispiecehere-butunfortunatelytheyhaveabadreputationbecausesomeimplementationsdon'tallowformattingandpeopleingeneraldon'tknowthatyoucandoit.


Here's a trivial example:
^(?:[^,]*+,){21}[^,]*+$ 

Which isn't really that difficult to read or maintain when it looks like this:
(?x) # enables comments, so this whole block can be used in a regex. ^ # start of string (?: # start non-capturing group [^,]*+ # as many non-commas as possible, but none required , # a comma ) # end non-capturing group {21} # 21 of previous entity (i.e. the group) [^,]*+ # as many non-commas as possible, but none required $ # end of string 

That's a bit of an over-the-top example (commenting $ is akin to commenting i++) but clearly there should be no problem reading and understanding that, and if maintenance is required it should be fine too.

RegularExpressionsarenoworsetoreadormaintainthananyotherunformattedcontent;indeedaregexisprobablyeasiertoreadthanthispieceoftexthere-butunfortunatelytheyhaveabadreputationbecausesomeimplementationsdon'tallowformattingandpeopleingeneraldon'tknowthatyoucandoit.

(Regular Expressions are no worse to read or maintain than any other unformatted content; indeed a regex is probably easier to read than this piece of text here - but unfortunately they have a bad reputation because some implementations don't allow formatting and people in general don't know that you can do it.)


Here's a trivial example:
^(?:[^,]*+,){21}[^,]*+$ 

Which isn't really that difficult to read or maintain anyway, but is even easier when it looks like this:
(?x) # enables comments, so this whole block can be used in a regex. ^ # start of string (?: # start non-capturing group [^,]*+ # as many non-commas as possible, but none required , # a comma ) # end non-capturing group {21} # 21 of previous entity (i.e. the group) [^,]*+ # as many non-commas as possible, but none required $ # end of string 

That's a bit of an over-the-top example (commenting $ is akin to commenting i++) but clearly there should be no problem reading, understanding, and maintaining that.


So long as you're clear as to when regular expressions are suited and when they're a bad idea, there's nothing wrong with them, and most times the JWZ quote doesn't really apply.
Source Link
Peter Boughton
  • 4.6k
  • 1
  • 32
  • 27

RegularExpressionsarenoworsetoreadormaintainthananyotherunformattedcontent;indeedaregexisprobablyeasiertoreadthatthispiecehere-butunfortunatelytheyhaveabadreputationbecausesomeimplementationsdon'tallowformattingandpeopleingeneraldon'tknowthatyoucandoit.


Here's a trivial example:
^(?:[^,]*+,){21}[^,]*+$ 

Which isn't really that difficult to read or maintain when it looks like this:
(?x) # enables comments, so this whole block can be used in a regex. ^ # start of string (?: # start non-capturing group [^,]*+ # as many non-commas as possible, but none required , # a comma ) # end non-capturing group {21} # 21 of previous entity (i.e. the group) [^,]*+ # as many non-commas as possible, but none required $ # end of string 

That's a bit of an over-the-top example (commenting $ is akin to commenting i++) but clearly there should be no problem reading and understanding that, and if maintenance is required it should be fine too.