博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
提取大段文字中的特殊段落
阅读量:4346 次
发布时间:2019-06-07

本文共 4664 字,大约阅读时间需要 15 分钟。

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
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
temp = screen.GetResult(content); for (int i = 0; i < temp.Count; i++) { Console.WriteLine(temp[i]); } } } }}
View Code

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;            }        }    }}
View Code

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;            }        }    }}
View Code

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
result; public ScreenText() { string match = string.Format("(?<={0}).*?(?={1})", key_start, key_end); regex = new Regex(match); result = new List
(); } 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
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; } }}
View Code

5:示例

@Key:a:b

@End:true

@File:xx.txt

 

转载于:https://www.cnblogs.com/Joke-crazy/p/9179041.html

你可能感兴趣的文章
第四阶段 15_Linux tomcat安装与配置
查看>>
NAS 创建大文件
查看>>
学习笔记-模块之xml文件处理
查看>>
接口测试用例
查看>>
Sybase IQ导出文件的几种方式
查看>>
linux 系统下 tar 的压缩与解压缩命令
查看>>
转:How to force a wordbreaker to be used in Sharepoint Search
查看>>
MySQL存储过程定时任务
查看>>
Python中and(逻辑与)计算法则
查看>>
POJ 3267 The Cow Lexicon(动态规划)
查看>>
设计原理+设计模式
查看>>
tomcat 7服务器跨域问题解决
查看>>
前台实现ajax 需注意的地方
查看>>
Jenkins安装配置
查看>>
深入理解Java虚拟机&运行时数据区
查看>>
02-环境搭建
查看>>
spring第二冲刺阶段第七天
查看>>
搜索框键盘抬起事件2
查看>>
透析Java本质-谁创建了对象,this是什么
查看>>
BFS和DFS的java实现
查看>>