using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Text.RegularExpressions;

public partial class StringFunctions
{
	static readonly Regex r1 = new Regex("\\((?<ac>[1-9][0-9][0-9])\\)");


	[SqlFunction(IsDeterministic = true, IsPrecise = true)]
	public static bool RegExMatch(string pattern, string matchString)
	{
		Regex r1 = new Regex(pattern.TrimEnd(null));
		return r1.Match(matchString.TrimEnd(null)).Success;
	}

	[SqlFunction(IsDeterministic = true, IsPrecise = true)]
	public static SqlString ExtractAreaCode(string matchString)
	{
		//Regex r1 = new Regex("\\((?<ac>[1-9][0-9][0-9])\\)");
		Match m = r1.Match(matchString);
		if (m.Success)
			return m.Value.Substring(1, 3);
		else return SqlString.Null;
	}
};

