The Artima Developer Community
Sponsored Link

.NET Buzz Forum
When Regular Expressions Attack

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Roy Osherove

Posts: 1807
Nickname: royo
Registered: Sep, 2003

Roy Osherove is a .Net consultant based in Israel
When Regular Expressions Attack Posted: Mar 30, 2004 10:59 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Roy Osherove.
Original Post: When Regular Expressions Attack
Feed Title: ISerializable
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/rosherove/Rss.aspx
Feed Description: Roy Osherove's persistent thoughts
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Roy Osherove
Latest Posts From ISerializable

Advertisement

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

The resulting pattern can be seen (fully commented) here:

    
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):

(?'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*\#
)

Read: When Regular Expressions Attack

Topic: More Israeli bloggers! Previous Topic   Next Topic Topic: Using for purposes other than disposable objects...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use