zoukankan      html  css  js  c++  java
  • [正则表达式]基础知识总结

      一直只是知道正则表达式的用途,但具体没使用过,今天稍微了解了下它的基础用法。这里总结一下。

      

      命令空间:System.Text.RegularExpressions

      用途:

      1.   对字符串进行查找和替换;
      2.   识别重复的单词;
      3.   转换格式;
      4.   区分一个字符串的各个子元素,并进行提取。

      RegEx类:

       是对正则表达式引擎的类抽象,通过调用该抽象类的不同方法,实现对正则表达式的处理。

       该类的主要方法如下所示,具体用途相信通过方法名即可知道:

      

                                 

      了解完Regex类的主要方法后,还需要关注三个比较重要的类和它们的层次,分别是:Match类、Group类和Capture类。

      假设有如下字符串,input(原始字符串),pattern(正则表达式).

      Match类:模式匹配返回的一个结果,比如调用Regex.Match()方法,会根据输入的正则表达式(pattern),在字符串(input)中进行匹配,每个匹配的结果返回一个Match类实例。

      Group类:表示正则表达式(pattern)中的子表达式的匹配结果,用括号括起来,如patter=@"(d{1,2})[.,]?(d{1,3})"。就有两个组:(d{1,2})和(d{1,3})。

      Capture类:返回匹配子表达式的结果。CaptureCollections返回所有符合子表达式的结果。

      Group类与Capture类的区别

      参考MSDN上的帮助文档(http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.group(v=vs.110).aspx),通过以下代码来说明它们的区别:

     1 using System;
     2 using System.Text.RegularExpressions;
     3 
     4 public class Example
     5 {
     6    public static void Main()
     7    {
     8       string pattern = @"((w+?)[,:;]?s?)+[?.!]";
     9       string input = "This is one sentence. This is a second sentence.";
    10 
    11       Match match = Regex.Match(input, pattern);
    12       Console.WriteLine("Match: " + match.Value);
    13       int groupCtr = 0;
    14       foreach (Group group in match.Groups)
    15       {
    16          groupCtr++;
    17          Console.WriteLine("   Group {0}: '{1}'", groupCtr, group.Value);
    18          int captureCtr = 0;
    19          foreach (Capture capture in group.Captures)
    20          {
    21             captureCtr++;
    22             Console.WriteLine("      Capture {0}: '{1}'", captureCtr, capture.Value);
    23          }
    24       }   
    25    }
    26 }
    27 // The example displays the following output: 
    28 //       Match: This is one sentence. 
    29 //          Group 1: 'This is one sentence.' 
    30 //             Capture 1: 'This is one sentence.' 
    31 //          Group 2: 'sentence' 
    32 //             Capture 1: 'This ' 
    33 //             Capture 2: 'is ' 
    34 //             Capture 3: 'one ' 
    35 //             Capture 4: 'sentence' 
    36 //          Group 3: 'sentence' 
    37 //             Capture 1: 'This' 
    38 //             Capture 2: 'is' 
    39 //             Capture 3: 'one' 
    40 //             Capture 4: 'sentence'

      

      如果在字符后添加了quantifiers(数量词),如在pattern表达式中,(w+?)用来匹配出现在一个句子当中的多个单词。但是Group对象只返回最后一个匹配(w+?)的对象。而Capture记录的是每一次匹配(w+?)的对象。如果没有添加数量词,Capture和Group可用来表示同一个对象。

      下图表示了三者的继承关系(左图),在使用时的层次关系(右图):

      接下来,就是介绍如何写正则表达式。这就是对不同字符串的分类和抽象表示的过程。主要包括以下9种类型:

    1. Character Escapes,转义字符;
    2. Character Classes:表示对字符的抽象分类;
    3. Anchors:锚点,界定字符位置;
    4. Group Construcs:对字符进行组合,使正则引擎想对待一个字符一样处理这个序列,用括号括起一个组;
    5. Quantifiers:数量词,定义字符出现的次数;
    6. Backreference Constructs:使用之前匹配的子表达式再次进行匹配;
    7. Alternation Constructs:选择匹配语句;
    8. Substitutions:字符替换;
    9. Regular Expression Options:正则表达式具体操作选项。

      

      理清楚这些类型之间的关系后,使用正则表达式就会有一个比较清晰的思路和概念。正则表达式的关键就是在于如何写正确的匹配语句,它是后续其它操作的基础。具体的类型格式可参考MSDN的:Quick Reference in PDF (.pdf) format。(具体下载链接:http://msdn.microsoft.com/en-us/library/hs600312.aspx)。

      下面附上一些自己练习的简单正则表达式,以方便有一个直观理解:

          1.将首字母替换成大写:

    1 string input = "this is a Good mornIng.";
    2 string pattern = @"([a-z])([a-zA-Z]{2,})";
    3 string result = Regex.Replace(input, pattern,
    match=>match.Groups[1].Value.ToUpper()+match.Groups[2].Value); 4 Console.WriteLine(result); 5 6 //结果:This is a Good MornIng.

      2.替换货币符号:

    1 string word = "$12.4 $0.2 $2 3.5";
    2 string sub = "$1";
    3 string pattern = @"p{Sc}*(d+[,.]?d*)";
    4 string result = Regex.Replace(word, pattern, "($1)");
    5 Console.WriteLine(result);
    6 
    7 //Result:
    8 //(12.4) (0.2) (2) (3.5)

      

  • 相关阅读:
    导包Scanner类---Java
    标准类组成---Java
    Educational Codeforces Round 54 (Rated for Div. 2) A. Minimizing the String
    [专题总结] 二分搜索专题
    [最大权森林] [POJ] 3723 Conscription
    [模板] 快读快写
    [模板] 常用数论 -> gcd、exgcd、乘法逆元、快速幂、快速乘、筛素数、快速求逆元、组合数
    [模板] 堆 heap
    牛客小白月赛8 F-数列操作 (有序表)
    [模板] 字符串处理函数
  • 原文地址:https://www.cnblogs.com/lsr-flying/p/4179131.html
Copyright © 2011-2022 走看看