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 的打开或访问。