October 23, 2006

FluentInterface Regular Expressions

Filed under: simplicity — syminfo @ 5:05 pm

Fluent InterfaceA technique for handling a constructor with a great many parameters. You chain together setter methods that return the object. This gives the effect of keyword=value. The catch is, the constructor can’t ensure consistency before the object is constructed.  (source)

Joshua Flanagan has built ‘ReadableRex‘, your ‘fluent interface’ into Regular Expressions. [Joshua's post]

His example walks you through creating a social security # pattern:  

    Regex socialSecurityNumberCheck = new Regex(Pattern.With.AtBeginning

        .Digit.Repeat.Exactly(3)

        .Literal(“-”).Repeat.Optional

        .Digit.Repeat.Exactly(2)

        .Literal(“-”).Repeat.Optional

        .Digit.Repeat.Exactly(4)

        .AtEnd);

About these ads

1 Comment »

  1. Thanks for the mention, but you copied the wrong code sample ;)
    The SSN check was the first example – a much simpler pattern.

    Comment by Joshua Flanagan — October 26, 2006 @ 10:38 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: