C# 文件读取(数据清洗)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.CodeDom.Compiler;
//引入正则表达式
using System.Text.RegularExpressions;

namespace 简单数据清洗
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //文件路径
            string filePath = @"C:\Users\21504\Desktop\作业\ConsoleApp1\ConsoleApp1";
            //正则表达式
            string pattern = @"(https?:)?//[\w./\-]+\.(png|jpg)";
            try
            {
                //尝试查找是有寻找的文件路径
                if (Directory.Exists(filePath))
                {
                    string[] files = Directory.GetFiles(filePath);
                    // 输出所有子级文件名
                    foreach (string file in files)
                    {
                        //找到需要过滤的文件
                        if (Path.GetFileName(file) == "示例.txt") {
                            string httpsName= File.ReadAllText(file);
                            Console.WriteLine(httpsName);
                            //正则查找匹配
                            MatchCollection matches = Regex.Matches(httpsName, pattern);
                            foreach (Match match in matches)
                            {
                                //判断是否存在https://没有加上
                                if (!match.Value.StartsWith("https://"))
                                {
                                    Console.WriteLine("https://" + match.Value);
                                }
                                else {
                                    Console.WriteLine(match.Value);
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                //尝试读取失败并说明结果
                Console.WriteLine("报错" + e.Message);
            }
            Console.ReadLine();
        }
    }
}

在URL中,斜杠 "/" 是用来分隔不同的路径部分的。在协议部分(例如 "https://")之后,通常是主机名(例如 "img2.kk.jpg")。斜杠之后可能跟着路径部分,以指示服务器上资源的位置。因此,URL中的 "//" 被认为是指示协议的一部分,而不是路径的一部分。在绝大多数情况下,多个连续的斜杠(例如 "")会被解释为单个斜杠。这是因为在URL解析中,连续的斜杠被视为一个单一的路径分隔符。因此,"https:img2.kk.jpg" 与 "https://img2.kk.jpg" 是等效的,两者都指向相同的资源。所以,多个斜杠不会影响 URL 的打开或访问。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值