Attention: We are retiring the ASP.NET Community Blogs. Learn more >

ShowUsYour<Blog>

Irregular expressions regularly

  • Windows Authentication with Role based authorization

    I've done a fair bit with ASP.NET security using the FormsAuthentication provider but not much at all with Windows authentication.  I'm currently building an app. “out-of-hours” that needed to use integrated Windows authentication and also use Role based authorization based on the users Windows Group membership.  Here is a little piece of code that I put together to hook the Windows groups into the roles of the IPrincipal in the application:

  • Application.DoStuff

    I nice little post by Eric Gunnerson that talked how to add an odd-shaped splash screen to an application.  The most interesting thing that came out of this post for me was that he mentioned having to call Application.DoEvents to allow Windows to process the necessary messages to get the splash form to render properly.  Being a web-guy Application.DoEvents is something that I've never really come to terms with.  If you were to go under the hood of some of my winforms apps you would undoubtedly uncover a shot-gun approach to calling this method - i.e.:  sprayed at random.

  • Regex Reminders [1] - Replacement Operations

    A common application for regular expressions is to find and replace strings within a body of text. These operations range from something as simple as finding some text and removing it to locating and re-writing Hmtl tags. Let's take a quick look at the signatures of the .NET Regular Expression Replace overloads.

  • Regex Reminders #0 - Commenting Regular Expressions

    Free spacing and comment mode
    .NET Regular Expressions allow you to embed comments and whitespace within pattern strings making them clearer to 
    read (because of the whitespace), and easier to maintain (due to the modularity). To use this feature you must turn on the RegexOptions.IgnorePatternWhitespace flag. This can be achieved
    either by using the RegexOptions enumerated datatype or, via the (?x) mode modifier. Therefore the following
    two examples are functionally identical: Regex re = new Regex( @" # This pattern matches Foo (?i) # turn on insensitivity # The Foo bit \b(Foo)\b " , RegexOptions.IgnorePatternWhitespace ) ; Regex re = new Regex( @"(?x) # This pattern matches Foo (?i) # turn on insensitivity # The Foo bit \b(Foo)\b " ) ; Using IgnorePatternWhitespace option, comments can be embedded in one of two ways. Firstly, a raw '#'
    character can be used to mark the beginning of a comment and the comment runs up to the first NEWLINE
    (\n) character that is encountered. This can be easily achieved in C# with verbatim strings @"..."
    or in VB by appending newline characters within the string using Chr(10), vbCrLf or Environment.NewLine.
    Comments can also be declared using the (?#...) syntax. The following 3 snippets demonstrate examples of each of these methods... C# Verbatim Strings. using System; using System.Collections; using System.Text.RegularExpressions ; namespace Regex Snippets.Tests { public class CommentedCSharp { public static void Main() { Regex re = new Regex( @" # This pattern matches Foo (?i) # turn on insensitivity # The Foo bit \b(Foo)\b " , RegexOptions.IgnorePatternWhitespace ) ; for ( Match m = re.Match( "foo bar Foo" ) ; m.Success ; m = m.NextMatch() ) Console.WriteLine( m.Value + Environment.NewLine ) ; Console.ReadLine(); } } } VB.NET using (?#...) syntax. Option Strict On Imports System.Text.RegularExpressions Namespace Regex Snippets.Tests Module CommentedVBOne Public Sub Main() ' Demonstrates a VB Whitespace pattern using the '" (?#...) " & _ ' syntax Dim re As New Regex( _ " (?# This pattern matches Foo ) " & _ " (?i) (?# turn on insensitivity ) " & _ " (?# The Foo bit ) " & _ " \b(Foo)\b " _ , RegexOptions.IgnorePatternWhitespace) Dim m As Match = re.Match("foo bar foo") Dim s As String = "" While m.Success s &= (m.Value & Environment.NewLine) m = m.NextMatch() End While MessageBox.Show(s) End Sub End Module End Namespace VB.NET using appended newlines. Option Strict On Imports System.Text.RegularExpressions Namespace Regex Snippets.Tests Module CommentedVBTwo Public Sub Main() ' Demonstrates a VB Whitespace pattern using the '" (?#...) " & _ ' syntax Dim re As New Regex( _ " # This pattern matches Foo " & vbCrLf & _ " (?i) # turn on insensitivity " & vbCrLf & _ " # The Foo bit " & vbCrLf & _ " \b(Foo)\b " _ , RegexOptions.IgnorePatternWhitespace) Dim m As Match = re.Match("foo bar foo") Dim s As String = "" While m.Success s &= (m.Value & Environment.NewLine) m = m.NextMatch() End While MessageBox.Show(s) End Sub End Module End Namespace

  • Regex Reminders - A brief explanation

    Over the next couple weeks I'm going to post a series of regular expression examinations; each one will be a code intensive focus on a certain operations such as "Replace" or "Working with Matches and Groups".  The goal of the Regex Reminders is to end up with a lot of code for working with regex's that people can just use to "grab and go".  The series will also spend time looking at common regex techniques such as alternation and grouping techniques.