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",来进行匹配是成功的。

  • 相关阅读:
    大数相乘ADT(String)
    ubuntu16.04光流(.flo)转png格式图片
    7. Reverse Integer
    1. Two Sum
    Ubuntu16.04安装opencv3.2(cuda8.0)
    Ubuntu16.04安装PAPI API
    faster r-cnn demo.py理解
    window10远程连接ubuntu16.04
    lab_2 Selenium
    软件测试homework 3
  • 原文地址:https://www.cnblogs.com/OpenCoder/p/13713964.html
Copyright © 2011-2022 走看看