---
title: "When Regular Expressions Attack"
url: "https://osherove.com/blog/2004/3/30/when-regular-expressions-attack.md"
canonical: "https://osherove.com/blog/2004/3/30/when-regular-expressions-attack.html"
published: "2004-03-30T13:31:00"
updated: "2004-03-30T13:31:00"
tags: [".Net Quotations"]
author: "Roy Osherove"
description: "It's not that I'm BAD at regular expressions, I just learn enough to get me by when I need them. I'm nothing like Darren, who decided to do something like this : While reading the VB Language Spec today I decided to…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# When Regular Expressions Attack

By Roy Osherove March 30, 2004 In [.Net Quotations](https://osherove.com/blog/category/net-quotations)

**Historical article** · Originally published March 30, 2004; technical details may be outdated.

It's not that I'm BAD at regular expressions, I just learn enough to get me by when I need them.

I'm nothing like Darren, [who decided to do something like this](http://blogs.regexadvice.com/DNeimke/posts/873.aspx):

_While reading the VB Language Spec today I decided to attempt to follow the grammar syntax for matching DateTime Literals:_     [_http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfvbspec2\_4\_6.asp?frame=true_](http://msdn.microsoft.com/library/en-us/vbls7/html/vblrfvbspec2_4_6.asp?frame=true)

_The resulting pattern can be seen (fully commented) here:_     [_http://www.regexlib.com/REDetails.aspx?regexp\_id=638_](http://www.regexlib.com/REDetails.aspx?regexp_id=638)

OK. I can't resist. Here's the full pattern (and it looks great in [The Regulator](http://royo.is-a-geek.com/regulator)):

```
(?'DateLiteral'     (?# Per the VB Spec : DateLiteral ::= '#' [ Whitespace+ ] DateOrTime [ Whitespace+ ] '#' )

    \#\s*

    (?'DateOrTime'  (?# DateOrTime ::= DateValue Whitespace+ TimeValue | DateValue | TimeValue )

        (?'DateValue'
            
            (?# DateValue ::= Whitespace+ TimeValue | DateValue | TimeValue )
            (

                (?# DateValue ::= MonthValue / DayValue / YearValue | MonthValue - DayValue - YearValue )
                    
                    (?'Month'(0?[1-9])|1[0-2])      (?# Month 01 - 12 )
                    (?'Sep'[-/])                    (?# Date separator '-' or '/' )
                    (?'Day'0?[1-9]|[12]\d|3[01])    (?# Day 01 - 31 )
                    \k'Sep'                         (?# whatever date separator was previously matched )
                    (?'Year'\d{1,4})

                \s+

                (?# TimeValue ::= HourValue : MinuteValue [ : SecondValue ] [ WhiteSpace+ ] [ AMPM ] )

                    (?'HourValue'(0?[1-9])|1[0-9]|2[0-4])    (?# Hour 01 - 24 )
                    [:]
                    (?'MinuteValue'0?[1-9]|[1-5]\d|60)       (?# Minute 01 - 60 )
                    [:]
                    (?'SecondValue':0?[1-9]|[1-5]\d|60)?     (?# Optional Minute :01 - :60 )
                    \s*
                    (?'AMPM'[AP]M)?

            )    
            |
            (     
                (?# DateValue ::= MonthValue / DayValue / YearValue | MonthValue - DayValue - YearValue )

                   (?'Month'(0?[1-9])|1[0-2])      (?# Month 01 - 12 )
                   (?'Sep'[-/])                    (?# Date separator '-' or '/' )
                   (?'Day'0?[1-9]|[12]\d|3[01])    (?# Month 01 - 31 )
                   \k'Sep'                         (?# whatever date separator was previously matched )
                   (?'Year'\d{4})
            )     
            |
            (
                (?# TimeValue ::= HourValue : MinuteValue [ : SecondValue ] [ WhiteSpace+ ] [ AMPM ] )

                    (?'HourValue'(0?[1-9])|1[0-9]|2[0-4])    (?# Hour 01 - 24 )
                    [:]
                    (?'MinuteValue'0?[1-9]|[1-5]\d|60)       (?# Minute 01 - 60 )
                    [:]
                    (?'SecondValue':0?[1-9]|[1-5]\d|60)?     (?# Optional Minute :01 - :60 )
                    \s*
                    (?'AMPM'[AP]M)?
            )     

       )

    )

    \s*\#
)
```

## More in .Net Quotations

[All posts in .Net Quotations](https://osherove.com/blog/category/net-quotations)

- Apr 3, 2004

    ### [True Binary Serialization and Compression of Datasets](https://osherove.com/blog/2004/4/3/true-binary-serialization-and-compression-of-datasets.html)

- Apr 3, 2004

    ### [DPack: Free enhancement Add-ins to VS.NET IDE](https://osherove.com/blog/2004/4/3/dpack-free-enhancement-add-ins-to-vsnet-ide.html)

- Mar 28, 2004

    ### [Low defect rates are not a myth](https://osherove.com/blog/2004/3/28/low-defect-rates-are-not-a-myth.html)

- Mar 27, 2004

    ### [VS-Styler :Toggle and save your VS.Net font and color settings instantly](https://osherove.com/blog/2004/3/27/vs-styler-toggle-and-save-your-vsnet-font-and-color-settings.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
