Regular expression to find separator dots in formula

前端 未结 2 1329
南方客
南方客 2021-01-20 23:59

The C# expression library I am using will not directly support my table/field parameter syntax:

The following are table/field parameter names that are not directly suppo

2条回答
  •  自闭症患者
    2021-01-21 00:39

    I've written a completely new answer, now that the problem is clarified:

    You can do this in a single regex. It is quite bulletproof, I think, but as you can see, it's not exactly self-explanatory, which is why I've commented it liberally. Hope it makes sense.

    You're lucky that .NET allows re-use of named capturing groups, otherwise you would have had to do this in several steps.

    resultString = Regex.Replace(subjectString, 
        @"(?:             # Either match...
         (?       #  (and capture into backref )
          (?=\w*\p{L})    #  (as long as it contains at least one letter):
          \w+             #  one or more alphanumeric characters,
         )                #  (End of capturing group ).
         \.               #  then a literal dot,
         (?        #  (now capture again, into backref )
          (?=\w*\p{L})    #  (as long as it contains at least one letter):
          \w+             #  one or more alphanumeric characters.
         )                #  (End of capturing group ) and end of match.
        |                 # Or:
         \[               #  Match a literal [
         (?       #  (now capture into backref )
          [^\]]+          #  one or more characters except ]
         )                #  (End of capturing group ).
         \]\.\[           #  Match literal ].[
         (?        #  (capture into backref )
          [^\]]+          #  one or more characters except ]
         )                #  (End of capturing group ).
         \]               #  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);
    

提交回复
热议问题