2012-07-19 33 views
1

如何從使用正則表達式的行連續字符「_」的源字符串中提取以下內容。請注意,行連續字符必須是該行上的最後一個字符。 。此外,搜索應該從字符串的結尾開始,並在第一個「(」遇到終止那是因爲我只關心什麼發生在文本的末尾如何使用正則表達式連續提取文本?

通緝輸出:

var1, _ 
    var2, _ 
    var3 

來源:

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

回答

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) 
) 
" 
+0

這是否說明來正確關閉正則表達式的好友? – 2012-07-19 05:39:10

+0

@FabrícioMatté:是的。 – Cylian 2012-07-19 05:40:23

+0

正在測試它,很好地完成。 +1 – 2012-07-19 05:45:25