隐藏

.NET Core(C#)中使用正则表达式提取替换匹配字符串实例

发布:2023/1/3 22:37:46作者:管理员 来源:本站 浏览次数:314

1、C# 正则表达式

参考文档:C# 正则表达式(Regex)


2、Replace(String input, String input, MatchEvaluator evaluator, RegexOptions options)

Replace(String input, String input, MatchEvaluator evaluator, RegexOptions options)在指定的输入字符串中,将匹配正则表达式模式的指定最大字符串数替换为MatchEvaluator委托返回的字符串。


1)input 参数


要搜索匹配项的字符串。


2)pattern 参数


要匹配的正则表达式模式。


3)evaluator 参数


evaluator一个自定义方法,该方法检查每个匹配项,然后返回原始的匹配字符串或替换字符串。


4)options 参数


枚举值的一个按位组合,这些枚举值提供匹配选项。


RegexOptions


枚举值


描述


Compiled


8


指定正则表达式编译为 MSIL 代码,


而不是被解释。


已编译的正则表达式最大限度地提高运行时性能,


代价是会影响初始化时间。


在调用 Options 方法时,


不应将此值分配给


CompileToAssembly(RegexCompilationInfo[], AssemblyName)


属性。有关详细信息,


请参阅正则表达式选项文章中的“已编译的正则表达式”部分。


CultureInvariant


512


指定忽略语言中的区域性差异。 有关详细信息,


请参阅正则表达式选项文章中的“使用固定区域性的比较”部分。


ECMAScript


256


为表达式启用符合 ECMAScript 的行为。


该值只能与 IgnoreCase、Multiline 和 Compiled 值一起使用。


该值与其他任何值一起使用均将导致异常。


有关 ECMAScript 选项的详细信息,


请参阅正则表达式选项文章中的“ECMAScript 匹配行为”部分。


ExplicitCapture


4


指定唯一有效的捕获是显式命名或编号的 (?...) 形式的组。


这使未命名的圆括号可以充当非捕获组,


并且不会使表达式的语法 (?:...) 显得笨拙。


有关详细信息,请参阅正则表达式选项文章中的“仅显式捕获”部分。


IgnoreCase


1


指定不区分大小写的匹配。 有关详细信息,


请参阅正则表达式选项文章中的“不区分大小写匹配”部分。


IgnorePatternWhitespace


32


消除模式中的非转义空白并启用由 # 标记的注释。


但是,此值不影响或消除标记单独的正则表达式语言元素的开头的字符类、


数值量词或标记的空格。 有关详细信息,


请参阅正则表达式选项一文中的“忽略空格”部分。


Multiline


2


多行模式。 更改 ^ 和 $ 的含义,


使它们分别在任意一行的行首和行尾匹配,


而不仅仅在整个字符串的开头和结尾匹配。


有关详细信息,请参阅正则表达式选项文章中的“多行模式”部分。


None


0


指定不设置任何选项。


有关正则表达式引擎的默认行为的详细信息,


请参阅正则表达式选项文章中的“默认选项”部分。


RightToLeft


64


指定搜索从右向左而不是从左向右进行。


有关详细信息,请参阅正则表达式选项文章中的“从右到左模式”部分。


Singleline


16


指定单行模式。 更改点 (.) 的含义,


以使它与每个字符(而不是除 \n 之外的所有字符)匹配。


有关详细信息,请参阅正则表达式选项文章中的“单行模式”部分。


3、使用Replace()提取替换字符串

可以使用C#中Regex.Replace()提取html中指定内容并进行替换,如下,


using System;

using System.Text.RegularExpressions;


namespace ConsoleApplication

{

   class Program

   {

       static void Main(string[] args)

       {

           string content = "<p><strong>1、new 运算符</strong>:用于创建对象和调用构造函数。这个我们创建对象实例就比较常用了,比如:</p><pre class=\"prettyprint linenums cs\">     StringBuilder str=new  StringBuilder();\r\n\r</pre><p><strong>"

          + "2、new 修饰符</strong>:在用作修饰符时,new 关键字可以显式隐藏从基类继承的成员。简单的说,就是现在写的这个类,想写一个和基类中相同的成员,而不继承基类的。运用多态的特性时,也不会调用这个显示隐藏的方法。具体看下如下代码:"

          + "</p><pre class=\"prettyprint linenums\">using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace ConsoleApp2\r\n{\r\n    public class Program\r\n    {\r\n        static void Main(string[] args)\r\n        {\r\n            animal a = new animal();\r\n            a.name = \"animal\";\r\n            a.say();\r\n            cat c = new cat();\r\n            c.name = \"cat\";\r\n            c.say();\r\n            dog d = new dog();\r\n            d.name = \"dog\";\r\n            d.say();\r\n            sheep s = new sheep();\r\n            s.name = \"sheep\";\r\n            s.say();\r\n            animal a1 = new cat();\r\n            a1.say();\r\n            animal a2 = new dog();\r\n            a2.say();\r\n            animal a3 = new sheep();\r\n            a3.say();\r\n        }\r\n    }\r\n    class animal\r\n    {\r\n        public string name { get; set; }\r\n        public virtual void say()\r\n        {\r\n            Console.WriteLine(\"animal say\");\r\n        }\r\n    }\r\n    class cat : animal\r\n    {\r\n        public override void say()\r\n        {\r\n            Console.WriteLine(\"cat say\");\r\n        }\r\n    }\r\n    class dog : animal\r\n    {\r\n        public new void say()   //这个方法被显示隐藏了\r\n        {\r\n            Console.WriteLine(\"dog say\");\r\n        }\r\n    }\r\n    class sheep : animal\r\n    {\r\n    }\r\n\r\n}<br></pre><p><strong>";


           string pattern = "<pre class=\"prettyprint linenums\\s*([a-z]*?)\"\\s*>(.+?)</pre>";

           MatchEvaluator evaluator = new MatchEvaluator(WordScrambler);

           Console.WriteLine(Regex.Replace(content, pattern, evaluator,

                             RegexOptions.IgnoreCase|RegexOptions.Singleline));

           Console.ReadKey();

       }


       public static string WordScrambler(Match match)

       {

           Console.WriteLine("--------------");

           Console.WriteLine("match.Value = " + match.Value);

           Console.WriteLine("match.Groups[1] = " + match.Groups[1]);//匹配到的第1个分组

           Console.WriteLine("match.Groups[2] = " + match.Groups[2]);//匹配到的第2个分组

           Console.WriteLine("--------------");

           return "";

       }

   }

}