正则 简单学习

 

 

那个黑框白心 是 /b

  /// <summary>
        
/// 日期转换匹配
        
/// </summary>

         public   static   void  Main()
        
{
            
//匹配字符串 例如 1/30/2008
            String ZipRegex = @"(?<m>d{1,2})/(?<d>d{1,2})/(?<y>d{4})";
            
//匹配后的字符串  便成 2008-1-30                 
            string ZipReplace = @"${y}-${m}-${d}";
            
bool finesh = true;
            
while (finesh)
            
{
                
string input = Console.ReadLine();
                
if (input == "")
                
{
                    finesh 
= false;
                    
break;
                }

                
//一般示例  查看是否匹配
                Match m = Regex.Match(input, ZipRegex);
                
if (m.Success)
                
{
                    Console.WriteLine(
"Math Success");
                }

                
//一般示例 查看是否匹配
                if (Regex.IsMatch(input, ZipRegex))
                
{
                    Console.Write(
"ZIP is valid!");
                    
//替换示例  其中第三个参数可以是正则
                    Console.WriteLine(Regex.Replace(input, ZipRegex, ZipReplace));
                }

                
else
                
{
                    Console.Write(
"ZIP is invalid!");
                    Console.WriteLine(Regex.Replace(input, ZipRegex, ZipReplace));
                }

                
//可以使用如下代码遍历输入字符串的匹配集合
                MatchCollection matchs = Regex.Matches(input, ZipRegex);
                Console.WriteLine(
"匹配个数" + matchs.Count.ToString());
                
foreach (Match var in matchs)
                
{
                    Console.WriteLine(
"find   " + var.ToString() + "at   " + var.Index);
                }

                
// 通常,在您需要指定默认方式以外的方式时,需要实例化 Regex 类的实例。特别是在设置选项时。例如,要创建忽略大小写和模式空白区域的 Regex 实例,然后检索与该表达式匹配的集合,则应使用如下代码:
                Regex r = new Regex(ZipRegex, RegexOptions.IgnoreCase);
                MatchCollection mc 
= r.Matches(input);
                
for (int i = 0; i < mc.Count; i++)
                
{
                    Console.WriteLine(
"find   " + mc[i].ToString() + " at  " + mc[i].Index);
                }

            }

        }
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值