Bogdan
Bogdan

Reputation: 696

Regex pattern [TableName].[FieldName]

I have a string for a calculation field which contain fields, operators, functions, etc. I need to extract fields from this string. Fields are in format: [TableName].[FieldName], where TableName and FieldName mai contain spaces. I solved the problem for TableName and FieldName without spaces with regex:

    List<string> fieldsFound = new List<string>();
    string pattern = @"\[(\w*)(\])(\.)(\[)(\w*)(\])";
    foreach (Match match in Regex.Matches(formula, pattern))
    {
        fieldsFound.Add(match.Value);
    }

So, I need a pattern which can allow spaces inside TableName and FieldName. Thanks!

Upvotes: 0

Views: 285

Answers (1)

Avinash Raj
Avinash Raj

Reputation: 174706

Put \w and \s into a character class inorder to match both word and space characters.

\[([\w\s]*)(\])(\.)(\[)([\w\s]*)(\])

DEMO

OR

Use the below regex if you don't want to capture leading, trailing spaces inside the [],

\[\s*([\w\s]*?)\s*\]\.\[\s*([\w\s]*?)\s*\]

DEMO

Upvotes: 2

Related Questions