Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Learning Regular Expressions: Readability

 
Ranch Hand
Posts: 282
Eclipse IDE PHP Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a software developer, it's important to make your code readable so that other developers (as well as your future self) can understand what you've written.  However, complex regular expressions are notoriously difficult to read.  Do you have any tips for improving the readability of regular expressions?  Thank you!
 
Marshal
Posts: 70266
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to make a regex human‑readable at all?
 
Ranch Hand
Posts: 140
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Is it possible to make a regex human‑readable at all?



Not by definition but nothig stops us from documenting the given regex.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Angstadt wrote:As a software developer, it's important to make your code readable so that other developers (as well as your future self) can understand what you've written.  However, complex regular expressions are notoriously difficult to read.  Do you have any tips for improving the readability of regular expressions?  Thank you!



To make your code with regular expression readable, for bigger regex, you can break them into the smaller ones that are easily understood and document them as Campbell recently advised. That's how you can also independently use smaller parts of the regex and the bigger one when combined.
 
Aniruddha Kalburgi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Is it possible to make a regex human‑readable at all?



That is a good idea. So I came around an idea to develop a software which will take any regex as input and translate it to the plain English. To enhance, the support for all standards of regex like Unix standard regex, POSIX & Perl can be added.

This challenge can further be enhanced to NLP where the standard regex can be presented as a plain English explanation. Once the implementation for this starts working nearly accurate, it can then be enhanced to translate plain English explanation to the regex.

It'd be great to hear more thoughts around this idea.
 
Aniruddha Kalburgi
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Aniruddha Kalburgi wrote:

Campbell Ritchie wrote:Is it possible to make a regex human‑readable at all?



That is a good idea. So I came around an idea to develop a software which will take any regex as input and translate it to the plain English. To enhance, the support for all standards of regex like Unix standard regex, POSIX & Perl can be added.

This challenge can further be enhanced to NLP where the standard regex can be presented as a plain English explanation. Once the implementation for this starts working nearly accurate, it can then be enhanced to translate plain English explanation to the regex.

It'd be great to hear more thoughts around this idea.



To add more, such software can easily help developers / architects when integrated as a plugin in IDE.
 
Campbell Ritchie
Marshal
Posts: 70266
282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Bartender
Posts: 1197
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Angstadt wrote:As a software developer, it's important to make your code readable so that other developers (as well as your future self) can understand what you've written.  However, complex regular expressions are notoriously difficult to read.  Do you have any tips for improving the readability of regular expressions?  Thank you!



https://www.martinfowler.com/bliki/ComposedRegex.html
 
Ranch Hand
Posts: 44
1
Python VI Editor Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Michael Angstadt wrote:As a software developer, it's important to make your code readable so that other developers (as well as your future self) can understand what you've written.  However, complex regular expressions are notoriously difficult to read.  Do you have any tips for improving the readability of regular expressions?  Thank you!



Some implementations of RegEx support the VERBOSE mode, which allows one to document subgroups of the RegEx or even character by character -- if that is needed to make it clear.  Not only do some IDE's support testing a RegEx, there a number of web site that can be used to test a RegEx.
    Bookmark Topic Watch Topic
  • New Topic