Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 290023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:57:36+00:00 2026-05-12T05:57:36+00:00

I was reading an article put together by Martin Fowler regarding Composed Regular Expressions

  • 0

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;
    }
  }

Or this:

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;

Or even this:

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.

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?

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:57:36+00:00Added an answer on May 12, 2026 at 5:57 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After reading this article http://camendesign.com/code/developpeurs_sans_frontieres I have decided to follow what it says and
I was reading this article about Double-Checked locking and out of the main topic
I just finished reading this article on the advantages and disadvantages of exceptions and
I was reading through this article: http://aws.typepad.com/aws/2008/12/running-everything-on-aws-soocialcom.html And I was wondering if this was
After reading this article , I'm thinking it's not possible. However, I have an
After reading this article I made a point that int () yields 0 because
After reading this article Storing C++ template function definitions in a .CPP file ,
After reading this article about validating with service layer I have some doubts. First:
I'm reading this article about how to allow a self-signed cert to be used
I was reading this article about Dynamic Objects in C# 4.0 . In that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.