zoukankan      html  css  js  c++  java
  • C# 从字符串中提取指定字符类型的内容

    从一段字符串中,提取中文、英文、数字

    中文字符30Margin中文字符40HorizontalAlignment

    正则表达式:

     1     /// <summary>
     2     /// 英文字母与数字
     3     /// </summary>
     4     public const string LettersAndNumbers = "[a-zA-Z0-9]+";
     5 
     6     /// <summary>
     7     /// 中文字符
     8     /// </summary>
     9     public const string ChineseChars = "[u4E00-u9FA5]+";
    10 
    11     /// <summary>
    12     /// 英文字符
    13     /// </summary>
    14     public const string EnglishChars = "[a-zA-Z]+";

     PS:使用正则匹配字符内容,不能使用开始、结束字符( ^文本开始; $文本结束)。

    Regex使用:

    1 string ChineseChars = "[u4E00-u9FA5]+";
    2 var match = Regex.Match("中文字符30Margin中文字符40HorizontalAlignment", ChineseChars, RegexOptions.IgnoreCase);
    3 var result = $"Index:{match.Index},Length:{match.Length}
    Result:{match.Value}";

    注:

    Regex.Match只会返回第一个匹配项

    如果需要获取正则对应的所有匹配项,可以使用 Regex.Matches

  • 相关阅读:
    面试(5)
    面试(五)
    面试(4)
    面试(四)
    面试(三)
    面试(2,3)
    利用session控制 长时间未操作自动退出登录
    一次性清除所有session
    动态引进js文件
    Vue--findIndex方法的使用原理
  • 原文地址:https://www.cnblogs.com/kybs0/p/11793332.html
Copyright © 2011-2022 走看看