正则表达式(14C#编程零基础到入门学习

本文作为上文的补充。由于意外关闭了浏览器,文章在修改的时候没有保存草稿,结果也没有写完,前文的定位点是跟正则表达式没有关系的,为了便于区分写到了一起做个对比,结果。。。

使用^和$定位点

完整代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace AnchorsUse
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string pattern = @"^s*This is a start of strings*.s*This is an end of strings*#34;;
            string input = "This is a start of string. This is an end of string";

            var match = Regex.Match(input, pattern);

            if (match.Success)
            {
                Console.WriteLine("Matched text: {0}", match.Value);
            }
            else
            {
                Console.WriteLine("No match found.");
            }
        }
    }
}
//注意空格

运行结果

使用A和Z定位点

完整示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace AnchorsUse2
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string pattern = @"AHelloZ";
            string pattern2 = @"A *Hello* Z";
            string input = "Hello";
            string input2 = " Hello123 ";

            var match = Regex.Match(input, pattern);

            if (match.Success)
            {
                Console.WriteLine("Matched text: {0}", match.Value);
            }
            else
            {
                Console.WriteLine("No match found.");
            }
            var match2 = Regex.Match(input2, pattern2);

            if (match2.Success)
            {
                Console.WriteLine("Matched text: {0}", match2.Value);
            }
            else
            {
                Console.WriteLine("No match found.");
            }
        }
    }
}
//建议手敲一遍

运行结果

注意空格 注意空格

使用的正则表达式是 AHelloZ,它会匹配只包含 "Hello" 的字符串。如果输入字符串是 "Hello123!",则不会匹配,因为 "Hello" 后面有 " 123"。如果只输入 "Hello",则匹配成功,并且 match.Value 将是 "Hello"。

下面的那个是有空格的“*”是匹配所有的,但是“string input2 = " Hello123 "”还有个123也匹配不成功。

使用b定位点

在C#中,正则表达式使用b来定位单词的边界。b表示单词的边界,即单词与非单词字符之间的位置。

例如,正则表达式bHellob将匹配字符串中的单词"Hello",但不会匹配字符串中的"HelloWorld"或"HELLO"。HelloA也是一样的。

完整示例代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace AnchorsUse3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string pattern = @"bHellob";
            string input = "Hello world!  HELLO";
            string pattern2 = @"bHelloAb";
            string input2 = "Hello world! HelloA  HELLO";

            var matches = Regex.Match(input, pattern);
            foreach (Match match in matches.Groups) 
            {
                Console.WriteLine(match.Value);
            }
            var matches2 = Regex.Match(input2, pattern2);
            foreach (Match match2 in matches2.Groups)
            {
                Console.WriteLine(match2.Value);
            }


        }
    }
}

运行结果

本文的正则表达式定位点和前文的定位点不是一回事。

展开阅读全文

页面更新:2024-02-15

标签:前文   上文   示例   空格   字符串   边界   单词   入门   本文   完整   代码   基础

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top