1:控制台
using ScreenFile.Script;
using System;
using System.Collections.Generic;
namespace ScreenFile
{
class Program
{
public static FileUtil fileUtil;
public static Command command;
public static ScreenText screen;
private static string inputValue;
private static void Main(string[] args)
{
fileUtil = new FileUtil();
command = new Command();
screen = new ScreenText();
while (inputValue != "Close")
{
inputValue = Console.ReadLine();
if (command.Check(inputValue))
{
Do_Command();
}
else if (screen.Check(inputValue))
{
Do_ScreenText();
}
}
}
private static void Do_Command()
{
command.ExecutiveCommand(inputValue);
}
private static void Do_ScreenText()
{
List<string> temp = screen.GetResult(inputValue);
for (int i = 0; i < temp.Count; i++)
{
Console.WriteLine(temp[i]);
}
}
public static void Do_Screen(string content)
{
if (screen.Check(content))
{
List<string> temp = screen.GetResult(content);
for (int i = 0; i < temp.Count; i++)
{
Console.WriteLine(temp[i]);
}
}
}
}
}
2:命令管理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ScreenFile.Script
{
class Command
{
private Regex regex;
public Command()
{
regex = new Regex("^@.*");
}
public bool Check(string command)
{
return regex.IsMatch(command);
}
public void ExecutiveCommand(string command)
{
command = regex.Match(command).Value.Trim();
string[] cmds = command.Split(':');
switch (cmds[0])
{
case "@Key":
Program.screen.ChangeRegex(cmds[1], cmds[2]);
break;
case "@End":
Program.screen.suffixName = cmds[1] == "true";
break;
case "@File":
string content = Program.fileUtil.Get_FileContent(cmds[1]);
Program.Do_Screen(content);
break;
default:
break;
}
}
}
}
3:读取本地文本
using System.IO;
namespace ScreenFile.Script
{
class FileUtil
{
public string Get_FileContent(string path)
{
using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
{
StreamReader sr = new StreamReader(fs);
string content = sr.ReadToEnd();
sr.Close();
return content;
}
}
}
}
4:正则匹配
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace ScreenFile.Script
{
class ScreenText
{
public string key_start = "https://";
public string key_end = ".html";
public bool suffixName;
private Regex regex;
private List<string> result;
public ScreenText()
{
string match = string.Format("(?<={0}).*?(?={1})", key_start, key_end);
regex = new Regex(match);
result = new List<string>();
}
public void ChangeRegex(string start, string end)
{
if (!string.IsNullOrEmpty(start))
key_start = start;
if (!string.IsNullOrEmpty(end))
key_end = end;
string match = string.Format("(?<={0}).*?(?={1})", key_start, key_end);
regex = new Regex(match);
}
public bool Check(string infomation)
{
return regex.IsMatch(infomation);
}
public List<string> GetResult(string infomation)
{
result.Clear();
foreach (Match mch in regex.Matches(infomation))
{
if (suffixName)
{
result.Add(mch.Value.Trim() + key_end);
}
else
{
result.Add(mch.Value.Trim());
}
}
return result;
}
}
}
5:示例
@Key:a:b
@End:true
@File:xx.txt