步步为营-18-正则表达式

说明:一个工具Regulator需要用到

一:正则表达式


 

1 what & why

  1.1 啥是正则?

    匹配字符串的规则。

  1.2 为什么要用正则?

    可以通过正则用来判断、寻找相应的字符串。例如:表单验证,判断是不是手机号、爬虫,从网页中爬取相应信息。

2 基本语法

1 元字符

 . 匹配任意单个字符

* 匹配前面内容出现0或多次

+ 匹配前面内容出现1或多次

? 匹配前面内容出现0或1次; 将贪婪模式转化为非贪婪模式

  贪婪模式     李琪(liqi);史昱(shiyu);李志强(lizhiqiang);李晶(ljing)  \((.+)\)  (liqi);史昱(shiyu);李志强(lizhiqiang);李晶(ljing)

  非贪婪模式 李琪(liqi);史昱(shiyu);李志强(lizhiqiang);李晶(ljing)  \((.+?)\)  (liqi)(shiyu)(lizhiqiang)(ljing)

| 或者z|food 匹配z或者food

() a:可以改变优先级;b:可以分组

[] 匹配[]中国的字符出现的一次.如[0-9]

{} 匹配前面内容出现的次数; a:指定次数如zo{2}o出现2次 ;a:指定次数如zo{2,4} o出现至少2次,最多4次;

^ a:以某些字符串开头.如: ^abc 以abc开头 b:取反

$ 以某些字符串结尾.

2 简写表达式

\d 代表一个数字,等于[0-9];

\D 代表一个非数字,等于[^0-9]

\s 代表换行符,tab键等空白字符

\S 代表非空白字符

\w 匹配字母 数字 下划线 汉字等

\W   ^w

二:练习


0:匹配括号中的内容

李琪(liqi);史昱(shiyu);李志强(lizhiqiang);李晶(ljing) 匹配为 liqi;shiyu;lizhiqiang;ljing

 <script type="text/javascript">
        onload = function () {
            var str = "李琪(liqi);史昱(shiyu);李志强(lizhiqiang);李晶(ljing)";
            var reg = /([^\(\)]+)(?=\))/g;
            //var result = str.match(reg);//此时result是数组
            //var Result2 = result.join(";");//此时Result2 是   liqi;shiyu;lizhiqiang;ljing
            var result = str.match(reg).join(";");
            alert(result);
        }
    </script>
小括号

[] 中的内容  /([^\(\)]+)(?=\))/g

0.1 去掉括号中的内容在C# 代码中

string st = "陈霓;蔡齐貌;蔡梅芳(caimf);蔡文婷(caiwt)";

Regex reg = new Regex(@"(\(.*?\))");
st.FastnessCCUser = reg.Replace(st, "");

输出结果 "陈霓;蔡齐貌;蔡梅芳;蔡文婷"

1:从文件中提取文件名如:C:\abc\123\test.txt

先在regulator中测试

 

然后放在程序中

string path = @"C:\abc\123\test.txt";
            Match mc = Regex.Match(path, @".+\\([a-zA-Z0-9]+\.[a-zA-Z]+)");
            if (mc.Success)
            {
                Console.WriteLine(mc.Groups[1].Value);
            }
            Console.Read();
View Code

2:匹配年月日

代码中

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

namespace 正则表达式
{
    class Program
    {
        static void Main(string[] args)
        {
            string date = @"June             26,              2017";
            Match mc = Regex.Match(date, @"([a-zA-Z]+)\s+([0-9]+),+\s+([0-9]+)");
            if (mc.Success)
            {
                Console.WriteLine(mc.Groups[1].Value);
                Console.WriteLine(mc.Groups[2].Value);
                Console.WriteLine(mc.Groups[3].Value);
            }
            Console.Read();
        }
    }
}
View Code


3 匹配IP地址 192.168.100.5[port=21,type=ftp]或者192.16.10.10[port=21]

 static void Main(string[] args)
        {
            //string date = @"192.168.100.5[port=21,type=ftp]";
            string date = @"192.168.100.5[port=21]";

            Match mc = Regex.Match(date, @"(?<ip>\d{3}(\.\d{1,3}){3})\[(?<port>port=[0-9]{1,5})(,(?<type>(type=\w+)))?");
            if (mc.Success)
            {
                Console.WriteLine(mc.Groups["ip"].Value);
                Console.WriteLine(mc.Groups["port"].Value);
                Console.WriteLine(mc.Groups["type"].Value);
            }
            Console.Read();
        }
View Code

 4:匹配[]的内容

 string businessCondition = publish.BusinessCondition;
                                string pattern = @"([^\:\]]+)(?=\])";
                                Regex regex = new Regex(pattern, RegexOptions.IgnoreCase);
                                MatchCollection matchs = regex.Matches(businessCondition);
                                if (matchs!=null&&matchs.Count>0)
                                {
                                    foreach (var item in matchs)
                                    {
                                        if (item!=null)
                                        {
                                            string wordKey = item.ToString().ToLower();
                                            conditionWords.Add(wordKey);
                                        }
                                       
                                    }

                                }
View Code

 

posted @ 2017-04-16 21:11  逍遥小天狼  阅读(245)  评论(0编辑  收藏  举报