VR引擎第十三天

96 阅读1分钟

断言 就是帮我们定位找到满足的字串 定位的位置左边和右边

左边断言 (?<=条件)匹配出来的字符串必须满足左边某个条件的正则 (?<!条件)

            //string patt1 = @"(?<!c)\d+";//括号中不参与匹配,只是帮我们定位
            //string patt2 = @"(?<=3).+(?=1)";
            //MatchCollection matches = Regex.Matches(str1, patt2);
            //Console.WriteLine(1);
            /*foreach (Match i in matches)
            {
                Console.WriteLine(i);
            }*/

右边断言 (?=条件) (?!条件) 帮我们定位子串必须满足右边某个正则条件的字符

练习一现在把你当前文件夹目录中的所有txt文件匹配

方法一

          string[] str2 = Directory.GetFiles("./");
            foreach (string i in str2)
            {
                Console.WriteLine(i);
            }
            string patt3 = @"(?<=\./).+(?=\.txt)"; // \转义
            foreach (string i in str2)
            {
                Match match = Regex.Match(i, patt3);
                if (match.Success) Console.WriteLine(match);
                //match.success 匹配成功就输出
            }

image.png

方法二 使用()去匹配我们要的子串

            string patt = @"./(.+)\.txt$";
            string[] files = Directory.GetFiles("./");
            foreach (string i in files)
            {
                Match match1 = Regex.Match(i, patt);
                Console.WriteLine(match1.Groups[0]);//下标0就是我们匹配到的子串
                Console.WriteLine(match1.Groups[1]);// 下表1表示第一个括号中的匹配的内容
            }
       

image.png

方法三 获取中间子串 (?<占位符>正则*?)

        string[] files = Directory.GetFiles("./");
            string patt = @"\./(?<fileName>.+).txt$";
            foreach (string i in files)
            {
                Match macth = Regex.Match(i, patt);
                Console.WriteLine(macth.Groups["fileName"]);//通过刚才的占位符名字拿到对应括号中匹配的内容
            }   

image.png

json 作用用来传输数据

在使用json的时候我们要下载json包 在引用using LitJson;

使用数组的形式获取json数据

    public class Student
    {
        public string name;
        public int age;
        public string sex;
        public override string ToString()
        {
            return $"name:{name},age:{age},sex:{sex},Hometown:{hometown}";
        }
    }
          Student[] student = JsonMapper.ToObject<Student[]>      (File.ReadAllText("./json.txt")); 

在json.txt文件下我们写的必须是数组而且里面字段的类型要和我们定义的一样

当我们拿到的是对象

      Hass student = JsonMapper.ToObject<Hass>(File.ReadAllText("./json.txt"));
      Console.WriteLine(student.person.name);
     public class Hass
    {
        public Person person;
    }
    public  class Hometown
    {
        public string province;
        public string city;
        public string county;
        public override string ToString()
        {
            return $"province:{province},city:{city},county:{county}";
        }
    }
    public class Person
    {
        public string name;
        public int age;
        public string sex;
        public Hometown hometown;
        public override string ToString()
        {
            return $"name:{name},age:{age},sex:{sex},Hometown:{hometown}";
        }
    }

文件中必须是对象

通过json写入到文件中

        using LitJson;
namespace json1
{
    public class Animal2
    {
        public string name;
        public int age;
        public override string ToString()
        {
            return $"name:{name},age:{age}";
        }
    }
    public class Animal
    {
        public Animal(string name,int age)
        {
            this.name = name;
            this.age = age;
        }
        public string name;
        public int age;
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Animal animal = new Animal("小黑", 18);//中文转成unicode编码格式
            string json = JsonMapper.ToJson(animal);
            //Console.WriteLine(json);
            File.WriteAllText("./1.txt", json);
            Animal2 cat = JsonMapper.ToObject<Animal2>(File.ReadAllText("./1.txt"));
            Console.WriteLine(cat);
            //把数组形式写入到json里面
            int[] array = new int[] { 1, 2, 3, 4 };
            string json1 = JsonMapper.ToJson(array);
            File.WriteAllText("./2.txt", json1);
        }
    }

集合类 有ArrayList动态数组 List列表 Dictionary字典 栈 队列 哈希表

            //创建一个栈
            //Stack stack = new Stack();
            //入栈
            //stack.Push(1);//入栈的数据类型可以不相同
            //stack.Push("ddd");
            //stack.Push(new Person());//储存对象
            //擦看栈中的值 只能读到栈顶的值
            //object i = stack.Peek();
            //Console.WriteLine(i);//peek是查看的方法
            /*int a = stack.Count;
            for (int j =0;j<a;j++)//获取栈的长度 stack.count
            {
                object i = stack.Peek();
                stack.Pop();
                Console.WriteLine(i);
            }*/
            /*while(stack.Count>0)
            {
                object i = stack.Peek();
                Console.WriteLine(i);
                stack.Pop();
            }*/
            //查看是否在栈中 返回值为bool值 //stack.Contains
            //清空栈 stack.clear();
            //Console.WriteLine(stack.Contains(1));

练习一编写一个方法计算任意一个十进制的二进制数 使用栈的结构方式储存

        Console.WriteLine("请输入一个十进制数");
            int a = Convert.ToInt32(Console.ReadLine());
            //Stack stack = new Stack();
            Stack stack = getNum(a);
            while (stack.Count > 0)
            {
                object i = stack.Peek();
                Console.Write(i);
                stack.Pop();
            }
        
        public static Stack getNum(int a)
        {

            Stack stack = new Stack();
            while (true)
            {
                int d = a / 2;
                int b = a % 2;
                if (b == 0)
                {
                    stack.Push(0);
                }
                if (b == 1)
                {
                    stack.Push(1);
                }
                if (d == 0)
                {
                    break;
                }
                a = d;
            }
            return stack;
         }

队列 Queue

         /*Queue queue = new Queue();
            //入队列
            queue.Enqueue("fss");
            queue.Enqueue(32);
            queue.Enqueue("name");
            //出队列
            //queue.Dequeue(); //出的是先进去的值
            //拿到队头的值
            Console.WriteLine(queue.Peek());//拿到队列头的值
            Console.Write(queue.Count);//长度
            while(queue.Count>0)
            {
                Console.WriteLine(queue.Peek());
                queue.Dequeue();
            }
            Console.WriteLine(queue.Contains(34));
            queue.Clear();
            Console.WriteLine(queue);
*/

练习二 使用队列储存信息,一次性存10条消息 每隔一秒打印一条消息,控制太打印消息时要有明显的停顿,每隔一秒使用Thread.Sleep(1000)

        //自己的方法
        Queue queue = new Queue();
            queue.Enqueue("请开始吃饭");
            queue.Enqueue("请开始哈哈哈");
            queue.Enqueue("请说话");
            queue.Enqueue("请的发射点");
            queue.Enqueue("分手的v");
            while(queue.Count>0)
            {
                Console.WriteLine(queue.Peek());
                queue.Dequeue();
                Thread.Sleep(1000);
            }

      //老师的方法
      Random random = new Random();
            List<string> news = new List<string>() { "我生气啦,要哄哄", "宝宝对不起,我错了", "哼", "错了错了" ,"好吧,我原来你"};
            Queue queue = new Queue();
            for(int i=0;i<10;i++)
            {
                string str = news[random.Next(0, news.Count)];
                queue.Enqueue(str);
            }
            while(queue.Count>0)
            {
                Console.WriteLine(queue.Peek());
                queue.Dequeue();
                Thread.Sleep(1000);
            }