110,538
社区成员
发帖
与我相关
我的任务
分享
using System.Text.RegularExpressions;
string str="姓名张三性别男年龄15姓名李四性别女年龄15";
string RegexString = @".*?姓名(?<Name>[^性别].*?)性别(?<Sex>[^年龄].*?)年龄(?<Age>[^姓名].*?).*?";
string[] Name = GetRegValue(str, RegexString, "Name", false);
string[] Sex = GetRegValue(str, RegexString, "Sex", false);
string[] Age = GetRegValue(str, RegexString, "Age", false);
Response.Write(Name[0].ToString());
/// <summary>
/// 正则表达式取值
/// </summary>
/// <param name="HtmlCode">源码</param>
/// <param name="RegexString">正则表达式</param>
/// <param name="GroupKey">正则表达式分组关键字</param>
/// <param name="RightToLeft">是否从右到左</param>
/// <returns></returns>
public string[] GetRegValue(string HtmlCode , string RegexString,string GroupKey,bool RightToLeft)
{
MatchCollection m;
Regex r;
if (RightToLeft == true)
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.RightToLeft);
}
else
{
r = new Regex(RegexString, RegexOptions.IgnoreCase | RegexOptions.Singleline);
}
m= r.Matches(HtmlCode);
string[] MatchValue = new string[m.Count];
for (int i = 0; i < m.Count; i++)
{
MatchValue[i] = m[i].Groups[GroupKey].Value;
}
return MatchValue;
}
class Program
{
static void Main(string[] args)
{
string[] aaa = xxxxx("姓名张三性别男年龄15姓名李四性别女年龄15");
foreach (string a in aaa)
{
Console.WriteLine("{0}", a);
}
Console.ReadLine();
}
static string[] xxxxx(string str)
{
string[] bbb = str.Split(new string[] { "姓名" }, StringSplitOptions.None);
for (int i = 0; i < bbb.Length; i++)
{
bbb[i] = bbb[i].Replace("年龄", " ").Replace("性别", " ");
}
return bbb;
}
}
string mysplit(string s)
{
return s.Replace("姓名","\n").Replace("年龄"," ").Replace("性别"," ");
}