C# 基于文本的应用 正则表达式

一 基于文本的应用

1 控制台应用程序

2 Main()函数的参数-命令行参数

① Main()函数可以带string[]参数;
② Main()函数可以有返回值(int),也可以为void;

二 使用Environment类

CommandLine  CommandLineArgs
MachineName  OSVersion
UserDomainName UserName
GetEnvironmentVariables
CurrentDirectory  SystemDirectory
GEtFolderPath(Environment.SpecialFolder.System)

三 文本处理常用的几个类

1 Console类

Write WriteLine ReadLine

2 String类

3 StringBuilder类

4 System.Text.Encoding类

Default.UTF8 GetEncoding
GetBytes(str) GetString(byte[])

四 正则表达式

1 正则表达式(Regular Expression)

2 用来表示匹配某类文本

如:
[0-9]{2,4}
1+$

3 正则表达式中几个主要要素

在这里插入图片描述
在这里插入图片描述

3 正则表达式的选项

在这里插入图片描述

五 Regex类

在这里插入图片描述

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

namespace 正则表达式查找电话号码
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string pattern = @"^[\.a-zA-z]+(?<name>\w+),[a-zA-z]+,[a-zA-z]+,x(?<ext>\d+)$";

            string[] sa =
            {
                "Dr.Dvid Jones,Ophthalmology,x2441",
                "Ms.Cindy Harriman,Registry,x6231",
                "Mr.Chester Addams,Mortuary,x1667",
                "Dr.Hawkeye Pierce,Surgery,x0986",
            };

            Regex rx = new Regex(pattern);

            foreach(string s in sa)
            {
                Match m = rx.Match(s);

                if (m.Success)
                    Console.Write(m.Result("${ext},${name},%1"));
                Console.WriteLine("\t" +
                    rx.Replace(s, "姓:${name},分机号:${ext}"));
            }
            Console.ReadKey();
        }
    }
}

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

namespace 正则表达式常见用法
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string pattern = "[Bbw]ill";
            string s = "My friend Bill will pay the bill";

            if (Regex.IsMatch(s, pattern))
                Console.WriteLine(s + "与" + pattern + "相匹配");

            Regex rx = new Regex(pattern);

            MachCollection mc = rx.Match(s);

            Console.WriteLine("有{0}次匹配", mc.Count);

            foreach(Match mt in mc)
            {
                Console.WriteLine(mt);
            }

            Match m = rx.Match(s);

            while(m.Success)
            {
                Console.WriteLine("在位置{0}有匹配'{1}",
                    m.Index, m.Value);
                m = rx.Match(s, m.Index + m.Length);
            }

            for(m=rx.Match(s);m.Success;m=m.NextMatch())
            {
                Console.WriteLine("在位置{0}有匹配'{1}",
                    m.Index, m.Value);
            }
        }
    }
}

播放歌曲显示歌词.rar: https://url09.ctfile.com/f/22158009-755093098-cec3bb?p=5939 (访问密码: 5939)


  1. a-z A-Z ↩︎

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值