I am trying to parse schema and table name using a regular expression. I am using C# (.NET Core) and System.Text.RegularExpressions.
Here are examples how the input can look like:
Schema.TableName
Schema . TableName
[Schema].[TableName]
[Schema] . [TableName]
Schema. [TableName]
[SchemaPart1.Part2.Part 3].[Table . Name]
And of course other variations that SQL Server accepts when you want to create a table using table and schema name.
Also, I am supporting other databases (MySql & Postgres), so I will reuse the same regex, but replace the [] with the right quotation character (` or " ). But I used the Sql Server examples because the start and ending quotation character are different.
My current regular expression looks like this:
\.?\[.+?\]|[^\[]+?(?=\.)|[^\[]+
This does work most of the times. But e.g. in this case [Schema] . [TableName] I get a match for whitespaces in between which I am not quite sure how to filter out.
Any help would be appreciated.
ParseSchemaObjectName()method seems to do what you want.^(?:\[[^]]+]|\w+) *\. *(?:\[[^]]+]|\w+).Multilineto the right before running the regex. regex101.com doesn't support .NET-style syntax per se, but it's so close to PCRE that it might be worth using that site for most of the cases; I find it easier to use for tweaking, since you see live results: regex101.com/r/h793L7/1.