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

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

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

  • 相关阅读:
    Win10删除anaconda重装
    anaconda python no module named 'past'的解决方法
    detectMultiScale 读取冲突的一个解决方法
    [原] Android快速开发框架-AndroidFine,GitHub开源
    [原] Android 自定义View 密码框 例子
    [原]发布一个jQuery提示框插件,Github开源附主站,jquery.tooltips.js
    [原] Jenkins Android 自动打包配置
    [原] Android性能优化方法
    阿里云9折推荐码:0LGVW2
    [原]那些年整理的Linux常用命令,简单明了
  • 原文地址:https://www.cnblogs.com/zhongqiang/p/5980298.html
Copyright © 2011-2022 走看看