我使用的C#表达式库不会直接支持我的表/字段参数语法:
以下是不直接支持的表/字段参数名称:
TableName1.FieldName1
[TableName1].[FieldName1]
[Table Name 1].[Field Name 1]
它接受不带空格的字母数字参数,或大括号括在方括号内的大多数字符。我想使用C#正则表达式将点分隔符和相邻括号替换为不同的分隔符,因此结果如下:
[TableName1|FieldName1]
[TableName1|FieldName1]
[Table Name 1|Field Name 1]
我还需要在单引号中跳过任何字符串文字,例如:
'TableName1.FieldName1'
当然,忽略任何数字文字,如:
12345.6789
编辑:感谢您就改进我的问题提出的反馈意见。希望现在更清楚了。
答案 0 :(得分:4)
我已经写了一个全新的答案,现在澄清了问题:
你可以在一个正则表达式中执行此操作。我认为它非常防弹,但正如你所看到的,它并不是完全不言自明的,这就是为什么我自由地评论它。希望它有意义。
你很幸运,.NET允许重复使用已命名的捕获组,否则你将不得不分几步完成这项工作。
resultString = Regex.Replace(subjectString,
@"(?: # Either match...
(?<before> # (and capture into backref <before>)
(?=\w*\p{L}) # (as long as it contains at least one letter):
\w+ # one or more alphanumeric characters,
) # (End of capturing group <before>).
\. # then a literal dot,
(?<after> # (now capture again, into backref <after>)
(?=\w*\p{L}) # (as long as it contains at least one letter):
\w+ # one or more alphanumeric characters.
) # (End of capturing group <after>) and end of match.
| # Or:
\[ # Match a literal [
(?<before> # (now capture into backref <before>)
[^\]]+ # one or more characters except ]
) # (End of capturing group <before>).
\]\.\[ # Match literal ].[
(?<after> # (capture into backref <after>)
[^\]]+ # one or more characters except ]
) # (End of capturing group <after>).
\] # Match a literal ]
) # End of alternation. The match is now finished, but
(?= # only if the rest of the line matches either...
[^']*$ # only non-quote characters
| # or
[^']*'[^']*' # contains an even number of quote characters
[^']* # plus any number of non-quote characters
$ # until the end of the line.
) # End of the lookahead assertion.",
"[${before}|${after}]", RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace);
答案 1 :(得分:-1)
希望您可以尝试使用此正则表达式:/(\w[0-9]* *)+/g
这会过滤掉除。之外的所有字母数字。