2013-05-10 83 views
0

正則表達式可以用行號獲取雙引號和單引號內的文本。正則表達式只能得到雙引號和單引號內的文本以及行號

例如:

line 123 Processing File "This is what i am looking for"<'this is also what i need'> 

輸出應該是:

line 123 This is what i am looking for this is also what i need 

我的正則表達式是:

MatchCollection matches2 = Regex.Matches(l, "\"([^\"]*)\""); 

    foreach (Match match2 in matches2) 
      { 
       foreach (Capture capture2 in match2.Captures) 
        { 
         textBox4.Text = textBox4.Text + capture2.Value + Environment.NewLine; 

        } 
      } 

我得到(我的輸出):

"This is what i am looking for" 

我不需要雙引號,我只需要引號內的文本。

+0

這豈不是可以使用'與string.replace( 「\」」, 「」)。替換( 「」「, 」「)'來刪除引號? – jgok222 2013-05-10 13:13:04

回答

0
string input = 
    "line 123 Processing File \"This is what i am looking for\"<'this is also what i need'>"; 
string output = 
    string.Join(" ", Regex.Matches(input, @"(line\s+(\d+)|(""|').*?\3)") 
          .Cast<Match>() 
          .Select(m => Regex.Replace(m.Value, 
                @"^[""']|[""']$", ""))); 

輸出:

line 123 This is what i am looking for this is also what i need 
+0

它的工作原理..但我也在雙引號之後創建文本.. **第283行 - // document。「this text」)。style.display =「none」; **我正在輸出:-this text style.display = none謝謝 – 2013-05-10 13:48:39

+0

我已修復編輯它。謝謝。 – 2013-05-10 13:58:14

+0

謝謝Lot.It的作品 – 2013-05-10 15:28:43

0

試試這個:

Regex.Replace(inputtext, @"line (\d+).*?\""(.*?)""\W+(.*?)'", "line $1 $2 $3") 
1

這裏的第一個問題是,你正在看比賽,而不是捕捉。捕獲將顯示()運算符收集的東西。

matches2.OfType<Match>().Select(x => x.Captures[0].Value) 
+0

可以使用正則表達式來獲取單引號和雙引號。我怎樣才能得到行號?謝謝 – 2013-05-10 13:23:59

相關問題