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

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

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


2
你没有提供足够的信息。“隔离”是一个普遍且相对的术语,你不能通过向一个已经模糊的术语添加条件来堆叠规格。请提供一些使用背景,并更加具体明确。 - user557597
我同意,我并没有明确的想法,你想要实现什么目标。也许你可以说明一下运行正则表达式后想要得到的结果,以及你不希望看到的结果(有时这非常有帮助)。 - ian
@sln,@Iain:我更新了我的初始帖子以扩展我的要求。@Tim: - polara
@Tim Pietzcker:使用C#正则表达式引擎。 我支持的唯一结构在我的示例中列出。 - polara
我猜这些结构体是在更大的上下文中找到的(即不是每个字符串/行一个结构体,而是与其他文本混合在一起)? - Tim Pietzcker
显示剩余2条评论
1个回答

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);

这太完美了,正是我所需要的。我非常感谢所有解释它如何工作的评论。希望这对于其他试图学习正则表达式的人有所帮助。谢谢你的帮助,Tim。 - polara

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接