匹配单个字符

单个字符 格式

  1. [set] 匹配 s、e、t
  2. [^set] 取反 不匹配s、e、t
  3. [a-d] 设置匹配范围 匹配a、b、c、d
  4. [^a-d] 匹配范围 取反 不匹配a、b、c、d
  5. .匹配 任意字符 不匹配 \n
  6. \char 匹配转义字符

案例

//1.[set]  匹配 s、e、t
string delimited = "[set]";
string input = "tuesday";
var matches = Regex.Matches(input, delimited);
PrintMatchResult(matches);

//2.[^set] 取反 不匹配s、e、t
delimited = "[^set]";
matches = Regex.Matches(input, delimited);
PrintMatchResult(matches);

//3.[a-d] 设置匹配范围 匹配a、b、c、d
input = "abecdf";
delimited = "[a-d]";
matches = Regex.Matches(input, delimited);
PrintMatchResult(matches);

//4.[^a-d] 匹配范围 取反 不匹配a、b、c、d
delimited = "[^a-d]";
matches = Regex.Matches(input, delimited);
PrintMatchResult(matches);

//5. 匹配任意字符 不匹配 \n
input = "\"abcd\nefg";
delimited = ".";
matches = Regex.Matches(input, delimited);
PrintMatchResult(matches);

//6. \char 匹配转义字符
delimited = "\"";
matches = Regex.Matches(input, delimited);
PrintMatchResult(matches);
//打印匹配结果
void PrintMatchResult(MatchCollection matches)
{
    Debug.Log($"<color=#00ff00>匹配数目:{matches.Count}</color>");

    foreach (Match match in matches)
    {
        Debug.Log("匹配結果:" + match.Value);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值