> Not too bad :-) But a problem is that you are using the * quantifier
with the brackets, so this would also match more than one bracket. Plus,
you don't need all the parens. It should also work like this:
\[?dbo\]?\.\[?[a-zA-Z0-9]+\]?
Ok to use this regular exression but i would like to add few scenarios where
this will fail.
1. When using [] the table name can contain special charecters and even
spaces?
2. And what about _ and other allowed charecters in table name?
3. Table names are case insensitive in SQL so DBO, Dbo, dbo and other
similar cases are also valid.
So my regular expression would be:
\[?dbo\]?\.((\[[^\]]+\])|([^\s]+))
Also note I have added the group capture () so that we can extract the
tablename when required.
<CODE>
string str = "Select * from DBO.Table_Name Where a = 3";
string pattern = @"\[?dbo\]?\.((\[[^\]]+\])|([^\s]+))";
Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
Match m = reg.Match(str);
if(m.Success)
{
string match = m.Groups[1].Value;
Console.WriteLine("Match Found: " + match);
}
else
{
Console.WriteLine("Match Not Found");
}
</CODE>
Here we will get
[Table Name]
in case using brackets, i have retained the brackets as this is necessary
when special charecters like space is used in table names.
OR
Table_Name
in other cases
Hope it will help.
--
Cheers,
Rahul Anand