RegExRestricted String (C# 2.0) – Regular Expression Restricted String Type

The RegExRestrictedString is a class that allow the developer to include simple validation into the type of a  string. This restriction and simple validation is often implemented as part of a database implementation for string length or format. It is intended that this type would enable ease of the transfer of simple validation from a back-end application to be used in conjunciton with a validator control like the ASP.NET Regular Expression Validator Control. At the end of this post I’ve included a sample implementation that shows the restriction of a string to 40 characters in length.



public abstract class RegExRestrictedString : IEquatable<RegExRestrictedString> {

    string internalValue;

    public RegExRestrictedString() { }

    public virtual RegexOptions Options {

        get { return RegexOptions.None; }


    protected abstract string Pattern { get; }

    public string Value {

        get { return internalValue; }

        set {


            this.internalValue = value;



    private void Validate(string value) {

        if (string.IsNullOrEmpty(Pattern)) throw new InvalidOperationException(

            "Pattern cannot be null or empty when assigning value.");

        Regex regEx = new Regex(Pattern, Options);

        if (regEx.Matches(value).Count != 1)

            throw new InvalidOperationException("Value does not match given restriction.");


    public override string ToString() {

        return internalValue;


    public override bool Equals(object obj) {

        return (obj is RegExRestrictedString) && Equals((RegExRestrictedString)obj);


    public override int GetHashCode() {

        return internalValue.GetHashCode()^Pattern.GetHashCode()^Options.GetHashCode();


    public bool Equals(RegExRestrictedString other) {

        if (object.ReferenceEquals(other, null)) return false;

        return ((Pattern == other.Pattern) && (Value == other.Value)

            && (Options == other.Options));


    public static bool operator ==(RegExRestrictedString first, RegExRestrictedString second) {

        if (object.ReferenceEquals(first, second)) return true;

        if (object.ReferenceEquals(first, null) || object.ReferenceEquals(second, null))

            return false;

        return ((first.Pattern == second.Pattern) && (first.Value == second.Value)

            && (first.Options == second.Options));


    public static bool operator !=(RegExRestrictedString first, RegExRestrictedString second) {

        return !first.Equals(second);





Sample implementation restricting a string to 0-40 characters in length that permits white space:

public class Comment : RegExRestrictedString {

    public Comment() { }

    public static implicit operator Comment(string value) {

        return new Comment().Value = value;


    protected override string Pattern {

        get { return "[a-zA-Z tnrfv]{0,40}"; }




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s