正则表达式(Regex)

作者:追风剑情 发布于:2017-1-9 14:17 分类:C#

示例一:匹配地点坐标


using System;
using System.Text.RegularExpressions;

namespace TestXML
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = @"我在这里<桃园村(158,63)>,他在那里<成都(1120,11)>";
            string pattern = @"<\S+?\(\d+,\d+\)>";

            Regex reg_pos = new Regex(pattern);
            string modified = reg_pos.Replace(text, "<地名(x,y)>");
            Console.WriteLine(modified);

            MatchCollection mc = Regex.Matches(text, pattern, RegexOptions.Multiline, TimeSpan.FromSeconds(1));
            Console.WriteLine("Count=" + mc.Count);
            foreach (Match m in mc)
            {
                Console.WriteLine("Value=" + m.Value);
            }
            Console.Read();
        }
    }
}


运行测试

11111.png

示例二:转换坐标格式(这里以转换成NGUI中需要的超链接形式为例)


using System;
using System.Text.RegularExpressions;

namespace TestXML
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = @"我在这里<桃园村(158,63)>,他在那里<成都(1120,11)>";
            string pattern = @"(<)(\S+?)\(\d+,\d+\)>";

            MatchCollection mc = Regex.Matches(text, pattern, RegexOptions.Multiline, TimeSpan.FromSeconds(1));
            foreach (Match m in mc)
            {
                ChatPosition pos = new ChatPosition(m.Value);
                Console.WriteLine(pos.ToString());
                Console.WriteLine(pos.GenerateLink());
            }
            Console.Read();
        }

        public class ChatPosition
        {
            private string mText;
            private string mName;
            private int mX;
            private int mZ;

            public ChatPosition(string text)
            {
                mText = text;
                try
                {
                    mName = text.Substring(1, text.IndexOf('(') - 1);
                    int startIndex = text.IndexOf('(') + 1;
                    int endIndex = text.IndexOf(',');
                    int.TryParse(text.Substring(startIndex, endIndex - startIndex), out mX);

                    startIndex = endIndex + 1;
                    endIndex = text.IndexOf(')');
                    int.TryParse(text.Substring(startIndex, endIndex - startIndex), out mZ);
                }catch(Exception e){
                    Console.WriteLine(e.StackTrace);
                }
                
            }

            public int X { get{return mX;} }
            public int Z { get { return mZ; } }

            public string GenerateLink()
            {
                string link = string.Format(@"[u][link=key_n=pos&id={0}&x={1}&z={2}][F86105]{3}[-][008800]({4},{5})[-][/link][/u]",
                    1001, mX, mZ, mName, mX, mZ);
                return link;
            }

            public string ToString()
            {
                return string.Format("Name={0}, X={1}, Z={2}", mName, mX, mZ);
            }
        }
    }
}


运行测试

22222.png

示例三:匹配IP

// 检查IP格式是否合法
public static bool IsValidIP(string ip)
{
	string pattern = @"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";
	Regex reg = new Regex(pattern);
	return reg.IsMatch(ip);
}

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号