zoukankan      html  css  js  c++  java
  • C#中换行符 正则表达式测试

    新建一个.NET Core控制台项目,敲入下面代码:

    using System;
    using System.Text.RegularExpressions;
    
    namespace NetCoreRegularEscapeDemos
    {
        class Program
        {
            static void Main(string[] args)
            {
                string text1 = "
     good day!";
                string pattern1 = "^
    .+$";
                string pattern2 = "^\n.+$";
                string pattern3 = "^\
    .+$";
    
                bool isMatched1 = false;
                bool isMatched2 = false;
                bool isMatched3 = false;
    
                isMatched1 = Regex.IsMatch(text1, pattern1);//true
                isMatched2 = Regex.IsMatch(text1, pattern2);//true
                isMatched3 = Regex.IsMatch(text1, pattern3);//true
    
                Console.WriteLine("isMatched1={0}", isMatched1);
                Console.WriteLine("isMatched2={0}", isMatched2);
                Console.WriteLine("isMatched3={0}", isMatched3);
    
                string text2 = "\n good day!";
                string pattern4 = "^\\n.+$";
                bool isMatched4 = false;
    
                isMatched4 = Regex.IsMatch(text2, pattern4);//true
                Console.WriteLine("isMatched4={0}", isMatched4);
    
                Console.WriteLine("Press any key to end...");
                Console.ReadKey();
            }
        }
    }

    运行结果如下所示:

    所以可以看到,实际上在C#中,字符串中的换行符" ",和正则表达式字符串中的" "、"\n"、"\ "都是匹配的。

    而C#字符串"\n",用正则表达式字符串"\\n",来进行匹配是成功的。

  • 相关阅读:
    《世界是数字的》
    IT小小鸟读书笔记
    Codeforces Round #665 Div.2 (CF1401)
    Codeforces Round #662 Div.2 (CF1392)
    std::array的效率问题
    CSS布局学习总结
    TCP中三次握手与四次挥手
    初见Vuex
    初见webpack
    CentOS7使用yum简便安装mysql5.7
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/13713964.html
Copyright © 2011-2022 走看看