我有一個大的字符串的HTML,具有下面的格式幾個標記:正則表達式 - 發現像火柴[屬性:GUID]
[Attribute:GUID]
因此,例如,有很多記號是這樣的:[Attribute:5c1670bb-b6ac-4fb5-92ab-9e0ea4bf72e8]
我該如何獲得所有匹配?我開始只是用[Attribute:d+]
,但顯然[
符號是一個正則表達式的令牌,我不能用,但我需要尋找它
我有一個大的字符串的HTML,具有下面的格式幾個標記:正則表達式 - 發現像火柴[屬性:GUID]
[Attribute:GUID]
因此,例如,有很多記號是這樣的:[Attribute:5c1670bb-b6ac-4fb5-92ab-9e0ea4bf72e8]
我該如何獲得所有匹配?我開始只是用[Attribute:d+]
,但顯然[
符號是一個正則表達式的令牌,我不能用,但我需要尋找它
我有一個大的字符串的HTML,具有下面的幾個標記 格式:
[Attribute:GUID]
因此,例如,有很多記號是這樣的:
[Attribute:5c1670bb-b6ac-4fb5-92ab-9e0ea4bf72e8]
我怎樣才能爲所有的比賽嗎?我開始只是用
[SiteAttribute:d+]
,但顯然[
符號是一個正則表達式令牌 ,我不能用,但我需要尋找它
如果你需要搜索文字「[」使用正則表達式中的轉義值「\ [」。
例如(類似你的 「SiteAttribute」 爲例):
"\[SiteAttribute:\d.*\]"
或者:
"\[Attribute:\d.*\]"
這取決於什麼可以成爲你的屬性。如果它可以是任何「字」組成的英文字母,它是:
\[[A-Za-z]+:[a-z0-9\-]+\]
注意,每個GUID
模式是無效的,所以圖案的檢查應予以考慮。
以下方法可以在兩種模式下調用,強制檢查模式中的GUID
有效性並調用而不檢查GUID
模式有效性。
private List<string> GetListOfValidAttributeGuids(string strInput, bool blGuidValidityCheck = true)
{
string strRegExAttrGuid = new Regex(@"\[Attribute:[a-zA-Z0-9-]+\]").ToString();
MatchCollection lstMatchedAttrGuid = Regex.Matches(strInput, strRegExAttrGuid);
List<string> lstMatchedAttrGuidValue = lstMatchedAttrGuid.Cast<Match>().Select(m => m.Value).ToList();
if(blGuidValidityCheck)
{
string strRegExGuid = new Regex(@":[a-zA-Z0-9\-]+").ToString();
List<string> lstValidMatchedAttrGuid = new List<string>();
foreach (string lst in lstMatchedAttrGuidValue)
{
MatchCollection validMatchedAttrGuid = Regex.Matches(lst, strRegExGuid);
string validMatchedAttrGuidValue = validMatchedAttrGuid.Cast<Match>().Select(match => match.Value).Single().Substring(1);
Guid guidTemp;
bool isValidGuid = Guid.TryParse(validMatchedAttrGuidValue, out guidTemp);
if (isValidGuid)
lstValidMatchedAttrGuid.Add(lst);
}
return lstValidMatchedAttrGuid;
}
return lstMatchedAttrGuidValue;
}
例如看二以下的方法調用:
string strInput = "In yek attribute mo'tabar hast: [Attribute:5c1670bb-b6ac-4fb5-92ab-9e0ea4bf72e8] " +
" va in yeki mo'tabar nist: [Attribute:7A7CE329-1338-4505-88B1-EA6B28BDE264E]";
List<string> call_01 = GetListOfValidAttributeGuids(strInput, false);
List<string> call_02 = GetListOfValidAttributeGuids(strInput, true);
這裏call_01
具有存儲在它元素,但call_02
具有一個元件。
**(\ {){0,1} [0-9A-FA-F] {8} \ - [0-9A-FA-F] {4} \ - [0-9A-FA-F ] {4} \ - [0-9A-FA-F] {4} \ - [0-9A-FA-F] {12}(\} **也可用於檢查GUID格式。 –
你能發佈更多數據嗎? –
它是一樣簡單['\ [屬性:([^] [] +)]'](http://regexstorm.net/tester?p=%5c%5bAttribute%3a(5B%5E%5D %%圖5b%5D%2b)的%5D與I =%5bAttribute%3a5c1670bb-b6ac-4fb5-92ab-9e0ea4bf72e9%5D +%5bAttribute%3a5c1670bb-b6ac-4fb5-92ab-9e0ea4bf72e8%5D +%5bAttribute%3a5c1670bb-b6ac-4fb5-92ab-9e0ea4bf72e7%圖5d) –