The power available in regular expressions always amazes me.

Been spending a portion of the day on the MS newsgroups helping people out.  I figure I'll get bonus points when I'm dead for helping people on Easter rather than searching for eggs and eating candy (just kidding my friends, just kidding).  I was starting into a long explanation of how to parse a specific string procedurally.  Basically, get your MatchCollection back, but then grab the beginning of the string and the end separately.  However, I'm better than that, and after determining how lazy I was being I just wrote the regular expression that would fix the guys problem rather than giving him the work-around that didn't force me to think.  In the end, the expression is much more elegant than the 2 page explanation of using Index and Length properties to find out the beginning and ending substrings that weren't matched by the expression.

So the lesson of the day is how to use \A and \Z.  They allow you to match the beginning and end of the input string.  Well, not quite, since there is a \z that allows you to truly match at the end of the string.  \Z allows matching at the end of the string or before \n at the end of the string.  Not much of a difference, but slight.  I'm using \Z in the pattern below, and based on the input I got, that should be the thing to do.

using System;
using System.Text.RegularExpressions;

public class SuperRegex {
private static string stuff =
@"00000000000000000000
200~11111111111111111
22222222222222222222222222222
3333333333333
200~444444444444444444444444
555555555555555555555555555555555
66666666
200~777777777777777777
888888888888";

    private static void Main(string[] args) {
        Regex regex = new Regex("(?ms)(?:\\A|^200~)(.*?)(?=^200~|\\Z)");
        MatchCollection matches = regex.Matches(stuff);
        for(int i = 0; i < matches.Count; i++) {
            Console.WriteLine();
            Console.WriteLine(matches[i].Value);
            Console.WriteLine();
        }
    }
}

Powerful stuff my friends, powerful stuff.  You can always check out RegexLib.com for even more regular expression goodies.  Darren is always keeping that place up to date with everything you could possibly want or need.

Published Sunday, April 11, 2004 3:14 PM by Justin Rogers

Comments

No Comments

Leave a Comment

(required) 
(required) 
(optional)
(required)