zoukankan      html  css  js  c++  java
  • 提取大段文字中的特殊段落

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

    5:示例

    @Key:a:b

    @End:true

    @File:xx.txt

  • 相关阅读:
    Openstack的项目管理方案
    Openstack的web管理端相关
    Openstack安全规则说明
    Openstack命令行删除虚拟机硬件模板flavor
    [转]java中的Static class
    [转]Activitys, Threads, & Memory Leaks
    [转]How to Leak a Context: Handlers & Inner Classes
    [原]Android打包之Eclipse打多渠道包
    [转]ubuntu下整合eclipse和javah生成jni头文件开发android的native程序
    Nao 类人机器人 相关资料
  • 原文地址:https://www.cnblogs.com/Joke-crazy/p/9179041.html
Copyright © 2011-2022 走看看