我想,註冊-Ex的表達式匹配的所有鍵 - 值Pairse你是whant是:
<Context>\s*?<Key>(.*?)\</Key>\s*?<Value>(.*?)</Value>\s*?</Context>
說明:
// <Context>\s*?<Key>(.*?)\</Key>\s*?<Value>(.*?)</Value>\s*?</Context>
//
// Match the characters "<Context>" literally «<Context>»
// Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?»
// Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the characters "<Key>" literally «<Key>»
// Match the regular expression below and capture its match into backreference number 1 «(.*?)»
// Match any single character that is not a line break character «.*?»
// Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the character "<" literally «\<»
// Match the characters "/Key>" literally «/Key>»
// Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?»
// Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the characters "<Value>" literally «<Value>»
// Match the regular expression below and capture its match into backreference number 2 «(.*?)»
// Match any single character that is not a line break character «.*?»
// Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the characters "</Value>" literally «</Value>»
// Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?»
// Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the characters "</Context>" literally «</Context>»
用法:
using System.Text.RegularExpressions;
public static void RunSnippet()
{
Regex RegexObj = new Regex("<Context>\\s*?<Key>(.*?)\\</Key>\\s*?<Value>(.*?)</Value>\\s*?</Context>",
RegexOptions.IgnoreCase | RegexOptions.Multiline);
Match MatchResults = RegexObj.Match(@"<ContextDetails>
<Context><Key>ID</Key><Value>100</Value></Context>
<Context><Key>Name</Key> <Value>MyName</Value></Context>
</ContextDetails>
");
while (MatchResults.Success){
Console.WriteLine("Key: " + MatchResults.Groups[1].Value) ;
Console.WriteLine("Value: " + MatchResults.Groups[2].Value) ;
Console.WriteLine("----");
MatchResults = MatchResults.NextMatch();
}
}
/*
Output:
Key: ID
Value: 100
----
Key: Name
Value: MyName
----
*/
的正則表達式來僅數學的價值或鍵「name」:
<Context>\s*?<Key>Name</Key>\s*?<Value>(.*?)</Value>\s*?</Context>
說明:
// <Context>\s*?<Key>Name</Key>\s*?<Value>(.*?)</Value>\s*?</Context>
//
// Match the characters "<Context>" literally «<Context>»
// Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?»
// Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the characters "<Key>Name</Key>" literally «<Key>Name</Key>»
// Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?»
// Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the characters "<Value>" literally «<Value>»
// Match the regular expression below and capture its match into backreference number 1 «(.*?)»
// Match any single character that is not a line break character «.*?»
// Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the characters "</Value>" literally «</Value>»
// Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.) «\s*?»
// Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
// Match the characters "</Context>" literally «</Context>»
用法:
string SubjectString = @"<ContextDetails>
<Context><Key>ID</Key><Value>100</Value></Context>
<Context><Key>Name</Key> <Value>MyName</Value></Context>
</ContextDetails>
";
Console.WriteLine(Regex.Match(SubjectString, "<Context>\\s*?<Key>Name</Key>\\s*?<Value>(.*?)</Value>\\s*?</Context>",
RegexOptions.IgnoreCase | RegexOptions.Multiline).Groups[1].Value);
你不應該使用正則表達式這個.. – ant 2010-07-06 07:38:16
這看起來並不像一個正則表達式我 - 什麼語言你用的是正則表達式嗎? Java的? 。淨? JavaScript的? Perl的?紅寶石?還有別的嗎? – Oded 2010-07-06 07:39:04
看起來像XML解析器的完美工作。 – 2010-07-06 07:39:41