Regex实例
2024-10-13 21:42:49
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions; namespace RegexTest
{
class Program
{
static void Main(string[] args)
{
string msg = "Welcome to China come comecome"; var matchs = Regex.Matches(msg, @"\bcome\b");
foreach (var item in matchs)
{
Console.WriteLine(item);
} msg = Regex.Replace(msg,@"\bcome\b","****");
Console.WriteLine(msg); string content = "Welcome to 'China' 'zoro' 'zore' 'zero'"; content = Regex.Replace(content, @"'(\w+)'","[$1]"); Console.WriteLine(content); string str = "我的生日是05/21/2010"; str = Regex.Replace(str, @"(\d+)\/(\d+)\/(\d+)", "$3-$2-$1"); Console.WriteLine(str);
Console.WriteLine("===================="); string str2 = "zxsssssh@itcast.cn"; string s = Regex.Match(str2,@"(.+)@").Groups[].Value;
Console.WriteLine(s);
string left = string.Empty;
for (int i = ; i < s.Length; i++)
{
left += "*";
} string ss = Regex.Replace(str2, @"(.+)@", left+"@");
Console.WriteLine(ss); Console.WriteLine("------------连线去重-----------"); string str3 = "佐佐罗罗";
str3 = Regex.Replace(str3, @"(.)\1+", "$1");
Console.WriteLine(str3); Console.WriteLine("------------查找出XXYY模式的叠词-----------"); string str4 = "浩浩荡荡、清清白白、AABB2、如火如荼、愈演愈烈、AXAY、MNYX,ABCD、没事找事、心服口服、AABBCC";
//查找出XXYY模式的叠词
MatchCollection matchs4 = Regex.Matches(str4, @"(.)\1(.)\2");
foreach (Match item in matchs4)
{
Console.WriteLine(item);
}
Console.WriteLine(matchs4.Count); Console.WriteLine("------------查找出XAXB模式的叠词-----------"); //查找出XAXB模式的叠词
MatchCollection matchs5 = Regex.Matches(str4, @"(.).\1.");
foreach (Match item in matchs5)
{
Console.WriteLine(item);
} Console.WriteLine("------------查找出ABCB模式的叠词-----------"); //查找出XAXB模式的叠词
MatchCollection matchs6 = Regex.Matches(str4, @".(.).\1");
foreach (Match item in matchs6)
{
Console.WriteLine(item);
} Console.WriteLine("------------查找出ABABAB模式的叠词-----------"); //查找出XAXB模式的叠词
MatchCollection matchs7 = Regex.Matches(str4, @"(.)\1(.)\2(.)\3");
foreach (Match item in matchs7)
{
Console.WriteLine(item);
} Console.ReadKey();
}
}
}
最新文章
- HTML&CSS日常知识点总结
- Delphi 各版本新特性功能网址收集
- shell return value
- MVC3 Model Binding验证方式
- 常见的HTTP状态码深入理解
- mongoDB之用户及权限设置
- 演练5-8:Contoso大学校园管理系统(实现存储池和工作单元模式)
- Vue跨门槛系列之实例的阐述
- NodeJs学习笔记(四)---单元测试
- 【数据库】数据库的锁机制,MySQL中的行级锁,表级锁,页级锁
- C# - 设计模式 - 虚模式
- blob canvas img dataUrl的互相转换和用处
- Vi编辑器中全局替换
- 杂谈c.cpp
- FTP实验
- spring4.0之三:@RestController
- WebStorm新创建项目介绍
- Qt5.3.2_CentOS6.4_单步调试环境__20160306【勿删,繁琐】
- VBA 公式中使用相对位置
- jquery正则判断字符串有几个逗号