2013-04-28 134 views
0

我需要使用正則表達式從字符串//table[@data-account='test']//span[contains(.,'FB')]得到//table[@data-account='test']使用正則表達式獲取字符串的一部分

我是新來的正則表達式,不能使用現有的樣本用於我的目的。 謝謝

+0

[本教程](http://www.regular-expressions.info/tutorial.html)是關於該主題的非常不錯的閱讀,應該讓您快速掌握正則表達式。這絕對是寶貴的知識,你肯定會重用。無論如何:一個輸入輸出對不足以讓我們判斷輸入的哪一部分是固定的,哪一部分是可變的,甚至可以證明這裏使用正則表達式是合理的。 – 2013-04-28 15:46:08

+0

它可以沒有正則表達式,它是你唯一的選擇嗎?你對其他方式感興趣嗎? – Mehran 2013-04-28 15:49:24

回答

0

你不需要正則表達式。你可以使用String.Split方法;

返回一個字符串數組,其中包含此字符串 中的子字符串,它們由指定字符串數組的元素分隔。

string s = @"//table[@data-account='test']//span[contains(.,'FB')]"; 
string[] stringarray = s.Split(new string[1] {@"//"}, StringSplitOptions.RemoveEmptyEntries); 
Console.WriteLine("//" + stringarray[0]); 

輸出將是;

//table[@data-account='test'] 

這裏是一個DEMO

0
using System; 
using System.Text.RegularExpressions; 

class P 
{ 
    static void Main() 
    { 
     Console.WriteLine(
      Regex.Match("//table[@data-account='test']//span[contains(.,'FB')]", "^([^]]+])").Groups[1].Value); 
    } 
} 
+0

哇!謝謝!驚人 – user2329418 2013-04-28 23:21:06

相關問題