返回
数据
分类

一个叫source.txt(有很多数据)

日期: 2020-03-13 23:06 浏览次数 : 175

缘起是因为一片帖子,问到了一个标题,帖子是如此的:

internal class Program
{
private static void Main(string[] args)
{

正则表达式相关文化,仿效MSDN文书档案: 和 

Originally Posted by 人就是那样
小编想编二个主次,但学CompSci是相当久以前的职业了。想请教请教我们。

var content = Read(@"E:workspace日志2016-06-21.LOG2016-06-21.LOG");
Regex reg3 = new Regex(@"手机号(?<result>(d+)?)");
var matches = reg3.Matches(content);
if (matches.Count > 0)
{
var list = new List<string>();
string phone;
foreach (Match item in matches)
必赢手机登录网址 ,{
phone = item.Result("${result}");
if (list.Contains(phone))
{

class Program
{
    static void Main(string[] args)
    {
        Regex reg = new Regex(@"^(?<fpath>([a-zA-Z]:\)([s.-w]+\)*)(?<fname>[w]+.[w]+)");

有三个txt文件,三个叫source.txt(有成都百货上千数量), 多个叫target.txt(空白的)

}
else
{
list.Add(phone);
}
}
}
}

        Match result = reg.Match(@"D:Program FilesAliWangWang7.10.07CemotionsTaoDollSniffer.dat");

自家想把source.txt里的有些数据提抽出来(稍稍改进一下),然后写到target.txt里面。

public static string Read(string path)
{
StreamReader sr = new StreamReader(path, Encoding.Default);
String line;
StringBuilder sb = new StringBuilder();
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);

        if (result.Success)
        {
            Console.WriteLine("[Full]:" + result.Value);

比方:
sourse.txt里的数量:
2oi)4@##( "data:001%abc">dsi-23)(*32##("data:dce%xy3"#(*EOIj2308Eld

}
return sb.ToString();

            Console.WriteLine("[Part1]:" + result.Result("${fpath}"));
            Console.WriteLine("[Part2]:" + result.Result("${fname}"));
        }

想提取的数码正是橘青黛色的。
data:001%abc

}
}

        Console.ReadLine();
    }

全部领收取来之后,笔者还想把%换到*, 然后每条数据背后加个逗号","

简言之整理如下,方便未来调用(援助多后缀名卡塔尔:

末尾target.txt就相应那样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
using System.Text.RegularExpressions;

data:001*abc,
data:dce*xyz

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// 路线分类
        /// </summary>
        public enum RouteType
        {
            /// <summary>
            /// 仅文件名
            /// </summary>
            NameOnly = 0,
            /// <summary>
            /// 文件名(包罗后缀)
            /// </summary>
            FileName = 1,
            /// <summary>
            /// 仅后缀名(包涵.)
            /// </summary>
            FileSuffix = 2,
            /// <summary>
            /// 除文件名之外的路子
            /// </summary>
            PathExceptName = 3,
            /// <summary>
            /// 除后缀之外的路子
            /// </summary>
            PathExceptSuffix = 4
        }
        /// <summary>
        /// 程序入口函数
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            string strPath = @"D:Program FilesAdvanced PDF Password RecoverySniffer.dat.doc.bak.zip";

请问应该咋做呀?实在JAVA忘光了。求教~~

            Console.WriteLine(GetPartRoute(strPath, RouteType.NameOnly));
            Console.WriteLine(GetPartRoute(strPath, RouteType.FileName));
            Console.WriteLine(GetPartRoute(strPath, RouteType.FileSuffix));
            Console.WriteLine(GetPartRoute(strPath, RouteType.PathExceptName));
            Console.WriteLine(GetPartRoute(strPath, RouteType.PathExceptSuffix));

若果帮自身做的话付点工资也得以。
在此以前自身也面前境遇过形似的标题,总是通进度序描述的法子缓解,现在难题又聊到来了,于是放低姿态想想。有了上学期330编译原理的底工,何况做过轻易状态自动机现在,已经不行醒目这种文字管理的事体应该提交Regular Expression(正则表达式State of Qatar,只但是本人总因为正则表明式晦涩难懂,由此尚未理想的雕饰过。
于是乎本身就计划借这些时机把Regular Expression好好的熟知一下。结果开掘前后相继原来那样好写:
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

            Console.ReadLine();
        }
        /// <summary>
        /// 正则获取文件路线
        /// </summary>
        /// <param name="strRoute">完整文件名</param>
        /// <param name="rType">要博得路线类型</param>
        /// <returns></returns>
        public static string GetPartRoute(string strRoute, RouteType rType)
        {
            string strResult = string.Empty;

namespace RegExpression
{
///
///
///
public class DataFilter
{
public static void Main(string[] args)
{
if( args.Length < 2 )
{
Console.Error.WriteLine("Please enter 2 filenames(e.g. In.txt Out.txt)");
return;
}
string Result;
using( StreamReader sr = new StreamReader(args[0]) )
{
Result = Filter( sr.ReadToEnd() );
}
using( StreamWriter wr = new StreamWriter(args[1]) )
{
wr.Write(Result);
}
}
private static string Filter(string input)
{
StringBuilder result = new StringBuilder();
Regex r = new Regex(""(?\w+):(?\w+)%(?\w+)"", RegexOptions.Compiled);
for( Match m = r.Match(input); m.Success; m = m.NextMatch() )
{
result.Append( m.Result("${data}:${key}*${value},"+Environment.NewLine) );
}
return result.ToString();
}
}
}

            Regex regex = new Regex(@"^(?<fpath>([a-zA-Z]:\)([s.-w]+\)*)(?<fname>[w]+)(?<namext>(.[w]+)*)(?<suffix>.[w]+)");

兑现这么些效应的严重性代码也就不超越10行就够了,三个字,爽。
略加改过:
·using statement
·end of line (Environment.NewLine)
·use StringBuilder to improve performence
那几个要多谢cumcum授予指正。

            Match result = regex.Match(strRoute);

            if (result.Success)
            {
                switch (rType)
                {
                    case RouteType.NameOnly:
                        strResult = result.Result("${fname}") + result.Result("${namext}");break;
                    case RouteType.FileName:
                        strResult = result.Result("${fname}") + result.Result("${namext}") + result.Result("${suffix}"); break;
                    case RouteType.FileSuffix:
                        strResult = result.Result("${suffix}"); break;
                    case RouteType.PathExceptName:
                        strResult = result.Result("${fpath}"); break;
                    case RouteType.PathExceptSuffix:
                        strResult = result.Result("${fpath}") + result.Result("${fname}") + result.Result("${namext}"); break;
                    default:
                        strResult = strRoute; break;
                }
            }

            return strResult;
        }
    }
}

点此下载