C# 正则表达式模式[TableName]。[FieldName]
我有一个计算字段的字符串,其中包含字段、运算符、函数等。 我需要从这个字符串中提取字段。字段的格式为:[TableName].[FieldName],其中TableName和FieldName包含空格。 我用正则表达式解决了TableName和FieldName不带空格的问题:C# 正则表达式模式[TableName]。[FieldName],c#,regex,string,C#,Regex,String,我有一个计算字段的字符串,其中包含字段、运算符、函数等。 我需要从这个字符串中提取字段。字段的格式为:[TableName].[FieldName],其中TableName和FieldName包含空格。 我用正则表达式解决了TableName和FieldName不带空格的问题: List<string> fieldsFound = new List<string>(); string pattern = @"\[(\w*)(\])(\.)(\[)(\w*)
List<string> fieldsFound = new List<string>();
string pattern = @"\[(\w*)(\])(\.)(\[)(\w*)(\])";
foreach (Match match in Regex.Matches(formula, pattern))
{
fieldsFound.Add(match.Value);
}
List fieldsFound=new List();
字符串模式=@“\[(\w*)(\])(\.)(\[)(\w*)(\])”;
foreach(正则表达式中的Match.Matches(公式、模式))
{
fieldsFound.Add(匹配值);
}
所以,我需要一个模式,可以允许在TableName和FieldName中使用空格。
谢谢 将
\w
和\s
放入字符类,以便匹配单词和空格字符
\[([\w\s]*)(\])(\.)(\[)([\w\s]*)(\])
或
如果不想捕获[]
中的前导空格和尾随空格,请使用下面的正则表达式
\[\s*([\w\s]*?)\s*\]\.\[\s*([\w\s]*?)\s*\]
卓越!我不需要在[]中添加尾随空格。第一个解决方案就是我需要的。