簡體   English   中英

正則表達式在公式中查找分隔符點

[英]Regular expression to find separator dots in formula

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

編輯:感謝您就改進我的問題提出的反饋意見。 希望現在更清楚了。

我已經寫了一個全新的答案,現在澄清了問題:

可以在單個正則表達式中執行此操作。 我認為它非常防彈,但正如你所看到的,它並不是完全不言自明的,這就是為什么我自由地評論它。 希望它有意義。

你很幸運.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);

希望你可以試試這個正則表達式: /(\\w[0-9]* *)+/g這會過濾掉除了之外的所有字母數字。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM