The Artima Developer Community
Sponsored Link

.NET Buzz Forum
The Perverse Art of Regular Expressions

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

Posts: 661
Nickname: scottwater
Registered: Aug, 2003

Scott Watermasysk is an ASP.NET developers. He wrote the .Text blog engine.
The Perverse Art of Regular Expressions Posted: Oct 8, 2003 7:00 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Scott Watermasysk.
Original Post: The Perverse Art of Regular Expressions
Feed Title: ScottW's ASP.NET WebLog
Feed URL: /error.aspx?aspxerrorpath=/blog/rss.aspx
Feed Description: ASP.NET and Blogging
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Scott Watermasysk
Latest Posts From ScottW's ASP.NET WebLog

Advertisement

For those of you who have not seen the .Text code, no ASPX pages exist except for the admin section. All of the blog pages are built on the fly. To decide which page to load, I have a custom set of HttpHandlers/HttpHandlerFactories.

A set of configurable Regular Expressions controls which Handler/Factory to load. I had kept my expressions rather simple, but as more and more blogs are created, I am noticing a need to extend some of the expressions.

Two items I neglected from my original patterns are "." and spaces. Space's will come later. We can focus on "." now.

Earlier today Donny IMed me saying his latest .Text update killed all blogs on DNJ with a "." in the blog name.

In short, I needed a Regular Expression that matched the following cases:

/Directory/Blog
/Directory/Blog/
/Directory/Blog/default.aspx

/Directory/Blog.Blog
/Directory/Blog.Blog/
/Directory/Blog.Blog/default.aspx

I also needed the expression not to match the following three cases:

/Directory
/Directory/
/Directory/default.aspx

A quick post to the ASPAdvice Regex List resulted in a couple of great suggestions.

The simplest and quickest seems to be by Wayne King:

If you simply want to verify that the string does not end with '.aspx', this will do that:

  $(?<!\.aspx)

If you want to verify there are exactly two directories specified, you can add to it to get:

  ^(/[\w.]+){2}.*$(?<!\.aspx)

A pattern that works precisely as you ask, "say \w|\. and NOT .aspx" is:

  \w|\.(?!aspx)


Which can be used to compose an alternate overall pattern that verifies there are exactly two directories:

  ^(/(\w+|\.(?!aspx))+){2}(/|/default\.aspx)?$


A huge thanks goes out to Wayne, Darren, and all of the others who quickly responded.

Read: The Perverse Art of Regular Expressions

Topic: A's Blow ALDS Game #4 Previous Topic   Next Topic Topic: Pragmatic Practitioners in Denver?

Sponsored Links



Google
  Web Artima.com   

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