csharp 正则表达式csharp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 正则表达式csharp相关的知识,希望对你有一定的参考价值。

using System;
using System.Text.RegularExpressions;

namespace regex_
{
    class Program
    {
        private static void showMatch(string text, string expr)
        {
            Console.WriteLine("The Expression: " + expr);
            MatchCollection mc = Regex.Matches(text, expr);
            foreach (Match m in mc)
            {
                Console.WriteLine(m);
            }
        }
        //matches words that start with 'S':
        private static void showMatch()
        {
            string str = "A Thousand Splendid kingSup Suns";

            Console.WriteLine("Matching words that start with 'S': ");
            showMatch(str, @"\bS\S*");
            Console.ReadKey();
        }
        // matches words that start with 'm' and ends with 'e':
        private static void showMatchStartEnd()
        {
            string str = "make maze and manage to measure it";

            Console.WriteLine("Matching words start with 'm' and ends with 'e':");
            showMatch(str, @"\bm\S*e\b");
            Console.ReadKey();
        }
        //replace extra white space
        private static void regReplace()
        {
            string input = "Hello   World   ";
            string pattern = "\\s+";
            string replacement = " ";
            Regex rgx = new Regex(pattern);
            string result = rgx.Replace(input, replacement);

            Console.WriteLine("Original String: {0}", input);
            Console.WriteLine("Replacement String: {0}", result);
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            regCaseInsencitive();
            Console.ReadKey();
        }

        private static void mathParticularString()
        {
            // First we see the input string.
            string input = "/content/alternate-1.aspx";

            // Here we call Regex.Match.
            Match match = Regex.Match(input, @"content/([A-Za-z0-9\-]+)\.aspx$",
                RegexOptions.IgnoreCase);

            // Here we check the Match instance.
            if (match.Success)
            {
                // Finally, we get the Group value and display it.
                string key = match.Groups[1].Value;
                Console.WriteLine(key);
            }
            Console.ReadKey();
        }
        private static void regCaseInsencitive()
        {
            //Using ToLower instead of RegexOptions.IgnoreCase on the Regex yielded a 10% or higher improvement.
            string input = "/content/alternate-1.aspx";

            // Here we lowercase our input first.
            input = input.ToLower();
            Match match = Regex.Match(input, @"content/([A-Za-z0-9\-]+)\.aspx$");
            if (match.Success)
            {
                string key = match.Groups[1].Value;
                Console.WriteLine(key);
            }
            Console.ReadKey();
        }
        ///tips
        //You can add the RegexOptions.Compiled flag for a substantial performance gain at runtime. This will however make your program start up slower. With RegexOptions.Compiled we see often 30% better performance.
    }
}

以上是关于csharp 正则表达式csharp的主要内容,如果未能解决你的问题,请参考以下文章

csharp 正则表达式

csharp 正则表达式捕获城市,州邮编

csharp 字符串替换正则表达式

csharp RegExp c#的正则表达式

csharp 正则表达式(基本 - 使用WindowsForm).cs

正则表达式-Csharp 学习笔记