zoukankan      html  css  js  c++  java
  • 正则表达式入门案例C#

    ---恢复内容开始---

    在网上百度了好多关于正则表达式的,不过好多都是关于语法的,没有一个具体的案例,有点让人难以入门,毕竟我还是喜欢由具体到抽象的认识。所以我就在这先提供了一个入门小案例(学了了6个月左右的java,现在转型c#,可能有些细节不行,请指教)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading.Tasks;
    using Microsoft.SqlServer.Server;
    
    namespace ConsoleApplication6
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string myText = @"Thiscomprehensive compendium provides a broad and thorough investigation of all aspects of programming with ASP.NET.Entirely revised and updaeted for the fourth
                 release of .NET,this book will give you the information you need to master ASP.NET and build adynamic,successful,enterprise Web application.";
                 string pattern =@"n";
                MatchCollection myMathches = Regex.Matches(myText, pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture );
                
                foreach (Match nextMatch in myMathches)
                {
                    Console.WriteLine(nextMatch.Index);
                }
            }
        }
    }

    注意字符串前面的符号@(此处作用:表示其中转义字符“不”被处理)。要再运行时把传递给.NET正则表达式引擎,反斜杠()不应该被c#编译器解释为转义序列。如果要查找以序列ion结尾的字,就可以适用下面的代码:

    const string pattern=@"ion";

    现在我们运行过程序之后大体对正则的使用有所了解省下的就是正则表达式的语法:

    就提供一个连接吧,因为真的有好多语法介绍,官网也有哦。

    http://www.imooc.com/article/8419

    我相信看完这些语法之后,再看看这个就应该明白了

    嗯,到此就结束了。希望有助于对大家的快速入门

  • 相关阅读:
    SQL复杂查询和视图(2)
    SQL复杂查询和视图
    SQL语言概述
    元组演算与关系代数关系
    关系的元组演算
    线索二叉树
    二叉树的遍历
    树的基本概念
    【支付专区】之微信支付构建请求参数xml
    【支付专区】之微信支付请求数据签名
  • 原文地址:https://www.cnblogs.com/zhongqiang/p/5980298.html
Copyright © 2011-2022 走看看