StreamReader sr = new StreamReader(Application.StartupPath + "\config.txt", Encoding.Default); 读取下面的内容
string cityInfo = sr.ReadToEnd();
sr.Close();
//有一串内容
https://crm.189.cn:84/provPortal/staff/login/page?areaId=8420100&areaName=武汉市&provinceName=湖北&staffCode=wh38613&password=122222&prov=123
https://crm.189.cn:84/provPortal/staff/login/page?areaId=8520100&areaName=贵阳市&provinceName=贵州&staffCode=W1477901&password=2222222&prov=123
https://crm.189.cn:84/provPortal/staff/login/page?areaId=8440300&areaName=深圳市&provinceName=广东&staffCode=1229100186&password=22222&prov=123
https://crm.189.cn:83/provPortal/staff/login/page?areaId=8320100&areaName=南京市&provinceName=江苏&staffCode=JSDQZT_WXCS&password=22222&prov=123
https://crm.189.cn:84/provPortal/staff/login/page?areaId=8320200&areaName=无锡市&provinceName=江苏&staffCode=JSDQZT_WXCS&password=2222&prov=133
写正则 截取想要的值
string reg = "(?<t>areaId=(?<areaId>\d+?)&areaName=(?<areaName>[^&]*?)&provinceName=(?<provinceName>[^&]*?)&staffCode=(?<staffCode>[^&]*?)&password=(?<password>[^&]*?)&prov=(?<prov>[^&]*?)([\r\n]|$))";
匹配读取想要值得数量
MatchCollection regexCity = Regex.Matches(cityInfo, reg);
//循环获取
foreach (Match m in regexCity)
{
string areaNameNow = m.Groups["areaName"].Value;
htCity.Add(areaNameNow, m.Groups["t"].Value);
int i = lbCity.Items.Add(areaNameNow);
if (areaNameNow == areaName)
{
lbCity.SelectedIndex = i;
}
}
列二:
string t="areaId=8440300&areaName=深圳市&provinceName=广东&staffCode=1229100186&password=7A887A3546472D837A429F5B&prov=adss";
string reg = "(?<t>areaId=(?<areaId>\d+?)&areaName=(?<areaName>[^&]*?)&provinceName=(?<provinceName>[^&]*?)&staffCode=(?<staffCode>[^&]*?)&password=(?<password>[^&]*?)&prov=(?<prov>[^&]*?)([\r\n]|$))";
Match mCity = Regex.Match(t, reg);
areaId = mCity.Groups["areaId"].Value;
areaName = mCity.Groups["areaName"].Value;
provinceName = mCity.Groups["provinceName"].Value;
prov = mCity.Groups["prov"].Value;