1

如何使用正则表达式从使用行继续符“_”的源字符串中提取以下内容。请注意,行继续字符必须是该行的最后一个字符。此外,搜索应该从字符串的末尾开始,并在遇到的第一个“(”处终止。那是因为我只对文本末尾发生的事情感兴趣。

想要的输出:

var1, _
    var2, _
    var3

来源:

...
Func(var1, _
    var2, _
    var3
4

1 回答 1

6

试试这个

(?<=Func\()(?<match>(?:[^\r\n]+_\r\n)+[^\r\n]+)

解释

@"
(?<=             # Assert that the regex below can be matched, with the match ending at this position (positive lookbehind)
   Func             # Match the characters “Func” literally
   \(               # Match the character “(” literally
)
(?<match>        # Match the regular expression below and capture its match into backreference with name “match”
   (?:              # Match the regular expression below
      [^\r\n]          # Match a single character NOT present in the list below
                          # A carriage return character
                          # A line feed character
         +                # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
      _                # Match the character “_” literally
      \r               # Match a carriage return character
      \n               # Match a line feed character
   )+               # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   [^\r\n]          # Match a single character NOT present in the list below
                       # A carriage return character
                       # A line feed character
      +                # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
"
于 2012-07-19T05:31:45.993 回答