正则 简单学习

 

 

那个黑框白心 是 /b

  /// 
        
/// 日期转换匹配
        
/// 

         public   static   void  Main()
        
{
            
//匹配字符串 例如 1/30/2008
            String ZipRegex = @"(?d{1,2})/(?d{1,2})/(?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);
                }

            }

        }
 

你可能感兴趣的:(.net,基础常识)