Dim rx As New Regex(?<=\<id\>)(?<id>[^\<]+)(?=</id>).*?(?<=\<name\>)(?<name>[^\<]+)(?=</name>)$") Dim test As String = "<id>aa</id><name>cat</name>" Dim matches As MatchCollection = rx.Matches(text) For ctr As Integer = 0 To Matches.Count - 1 Console.WriteLine(matches.Item(ctr).Value) Next
#region 获得字符串中开始和结束字符串中间得值
/// <summary>
/// 获得字符串中开始和结束字符串中间得值
/// </summary>
/// <param name="begin">开始匹配标记</param>
/// <param name="end">结束匹配标记</param>
/// <param name="html">Html字符串</param>
/// <returns>返回中间字符串</returns>
public MatchCollection GetMidValue(string begin, string end, string html)
{
Regex reg = new Regex("(?<=(" + begin + "))[.\\s\\S]*?(?=(" + end + "))", RegexOptions.Multiline | RegexOptions.Singleline);
return reg.Matches(html);
}
#endregion
能说说vb如何写吗?
string str = "<id>aa</id><name>cat</name>";
string strS1 = "<id>";
string strS2 = "</id>";
string strId = str.Substring(str.IndexOf(strS1), str.IndexOf(strS2));
Response.Write(strId);
Dim test As String = "<id>aa</id><name>cat</name>"
Dim matches As MatchCollection = rx.Matches(text)
For ctr As Integer = 0 To Matches.Count - 1
Console.WriteLine(matches.Item(ctr).Value)
Next
问题解决!
因为你已经知道了分隔符号,这样直接用字符串截取方法更好些:
string str = "<id>aa</id><name>cat</name>";
string strS1 = "<id>";
string strS2 = "</id>";
string strId = str.Substring(str.IndexOf(strS1), str.IndexOf(strS2));
Response.Write(strId);