断言
找到满足的子串 定位位置左边和右边。 左边断言 (?<=条件)字符串必须满足左边某个条件的正则 (?<!条件); //右边断言 (?=条件) (?!条件) 帮我们定位
//左边断言 (?<=条件)字符串必须满足左边某个条件的正则 (?<!条件)
string str = "abc1234def123";
string pattern = @"(?<!c)\d+";//括号中不参与匹配,只定位
MatchCollection matches = Regex.Matches(str, pattern);
foreach (Match i in matches)
{
Console.WriteLine(i);//234 123
}
//右边断言 (?=条件) (?!条件) 帮我们定位
string str = "abc1234def1234";
string pattern = @"(?<=3).+(?=1)";//在1前面,3的后面
MatchCollection matches = Regex.Matches(str, pattern);
foreach (Match i in matches)
{
Console.WriteLine(i);//4def
}
//把当前文件目录下的文件
string pattern1 = @"(?<=\./).+( ?=\.txt)";
string[] files = Directory.GetFiles("./");
//MatchCollection matches = Regex.Matches(str, pattern1);
foreach (string i in files)
{
Match match = Regex.Match(i, pattern1);
if (match.Success) //match.Success匹配成功的时候就打印
Console.WriteLine(match);//1.txt 2.txt
}
//使用()匹配我们要的子串 字符串格式化 $"变量名占位"
string pattern1 = @"(\./)(.+)\.txt$";
string[] files = Directory.GetFiles("./");
foreach (string i in files)
{
Match match = Regex.Match(i, pattern1);
//Console.WriteLine(match.Groups[0]);//下标0为匹配到的子串
//我想拿到括号中的字符
Console.WriteLine(match.Groups[1]);//下标1为第一个括号匹配到的内容 // ./
Console.WriteLine(match.Groups[2]);//下标2为第二个括号匹配到的内容 // 1
}
//第三种方式获取中间子串(?<占位符>正则*?)
string pattern1 = @"\./(?<fileName>.+).txt$";//匹配左边是./ 右边是.txt且以.txt结尾
string[] files = Directory.GetFiles("./");
foreach (string i in files)
{
Match match = Regex.Match(i, pattern1);
Console.WriteLine(match.Groups["fileName"]);//1 2
}
Json
using LitJson;//一定要引入命名空间
public class Human {
public Person person;
}
public class Hometown
{
public string provinxe;
public string city;
public string county;
public override string ToString()
{
return $"provinxe:{provinxe},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} ";
}
}
public class Sthdent
{
public string name;//空字符串
public string id;//0
public override string ToString()//打印对象可以直接看到里面的键值对
{
return $"name:{name},id:{id}";
}
}
Sthdent stu = new Sthdent();
Console.WriteLine(stu);//name:,id:
Sthdent[] student = JsonMapper.ToObject<Sthdent[]>(File.ReadAllText("./json.txt"));
foreach (Sthdent i in student)
{
Console.WriteLine(i.name);//小明 小强
}
//如果json的根是对象,用对象的形式拿数据
Human student = JsonMapper.ToObject<Human>(File.ReadAllText("./jsons.txt"));
Console.WriteLine(student.person);//name:王安石,age:200,sex:男,Hometown:provinxe:,city:九江市,county:清华三号
//转字符串
public class Animal2
{
public string name;
public int age;
}
public class Animal
{
public Animal(string name,int age)
{
this.name = name;
this.age = age;
}
public string name;
public int age;
}
//对象转字符串
Animal animal = new Animal("小黑", 18);//中文转成unicode
string json = JsonMapper.ToJson(animal);
Console.WriteLine(json);//{"name":"\u5C0F\u9ED1","age":18}
int[] intArray = new int[] { 1, 2, 3, 4 };
string json = JsonMapper.ToJson(intArray);//替换1.txt的内容为intArray的内容
File.WriteAllText("./1.txt", json);
//1.txt的内容为[1,2,3,4]
Animal2 cate = JsonMapper.ToObject<Animal2>(File.ReadAllText("./1.txt"));
Console.WriteLine(cate.name);//小黑
集合类
//栈 注意:要引入命名空间 using System.Collections;
//创建一个栈
Stack stack = new Stack();
//入栈
stack.Push(1);//入栈的数据类型可以不同
stack.Push("ddd");
stack.Push(new Person());
//查看栈中的值 只能读到栈顶的值
object i = stack.Peek;//object可代表里面所有的类型
Console.WriteLine(i);//
int length = stack.Count;
for (int j = 0; j < length; j++)//如何获取栈的长度 stack.Count
{
object i = stack.Peek();
Console.WriteLine(i);//集合类.Person ddd 1
stack.Pop();
}
//第二种
while (stack.Count > 0)
{
object i = stack.Peek();
Console.WriteLine(i);//集合类.Person ddd 1
stack.Pop();
}
Console.WriteLine(stack.Contains("ddd"));//检查栈中是否包含某个元素,返回一个布尔值
//练习:输入一个十进制数,转成二进制取余数入栈
static void getBin(int num)
{
Stack stack = new Stack();
while (num > 0)
{
int remainer = num % 2;
//入栈
stack.Push(remainer);
num /= 2;
}
//出栈
while (stack.Count > 0)
{
Console.Write(stack.Peek());
stack.Pop();
}
}
Console.WriteLine("请输入一个十进制数");
int num = int.Parse(Console.ReadLine());
getBin(num);
//队列
Queue queue = new Queue();
//入队列
queue.Enqueue("ASD");
queue.Enqueue(213);
queue.Enqueue("name");
//出队列
//queue.Dequeue();//ASD已出
//拿到队列头的值
Console.WriteLine(queue.Peek());//213
Console.WriteLine(queue.Count);//2
//遍历
while (queue.Count > 0)
{
Console.WriteLine(queue.Peek());
queue.Dequeue();
}
Console.WriteLine(queue.Contains(213));
queue.Clear();//清空
Console.WriteLine(queue);
//练习
Random ran = new Random();
List<string> list = new List<string>() {"喜迎二十大","俄乌冲突","抗癌药", "喜迎十九大" , "喜迎十八大" };
Queue queue = new Queue();
while (queue.Count < 10)
{
string str = list[ran.Next(0,list.Count)];
queue.Enqueue(str);
}
while (queue.Count>0)
{
Console.WriteLine(queue.Peek());
queue.Dequeue();
Thread.Sleep(1000);
}