• 正则表达式的用法


    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;

  • 相关阅读:
    springboot~使用docker构建gradle项目
    CH BR8(小学生在上课-逆元和互质数一一对应关系)
    UNIX环境高级编程第二版代码笔记
    【Linux学习笔记】用nc实现两台主机间的文件传输(不需要输密码)
    hdu 1159
    轻量级的原型设计工具-Axure RP
    在Ubuntu 12.10 上安装部署Openstack
    [Android 中级]Voip之CSipSimple类库的编绎
    OpenStack云计算快速入门之一:OpenStack及其构成简介
    OpenStack云计算快速入门之二:OpenStack安装与配置
  • 原文地址:https://www.cnblogs.com/lacey/p/5555576.html
Copyright © 2020-2023  润新知