小编典典

正则表达式在公式中查找分隔点

all

我使用的 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

编辑:感谢您对改进我的问题的反馈。希望现在更清楚了。


阅读 105

收藏
2022-05-16

共1个答案

小编典典

既然问题已经澄清,我已经写了一个全新的答案:

可以在单个正则表达式中执行此操作。我认为它非常防弹,但正如你所看到的,它并不完全不言自明,这就是我对它进行自由评论的原因。希望这是有道理的。

您很幸运 .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);
2022-05-16