我在一个files数组中有一个string (.aspx,.cs,.html等)列表。我阅读了file的所有内容。一直到这里都没事!
我想要做的是搜索一个特定的字符串
EG:
<meta name="description" content="NOINDEX" />
<meta name="keywords" content="NOINDEX" />通过循环遍历文件列表,获取文件内容,并检查它是否searchstring. contains
foreach (string item in strFiles)
{
innerList = item.Split(',');
if(!string.IsNullOrEmpty(innerList[0]))
{
fileList.Add(innerList[0]);
fileContents = File.ReadAllText(innerList[0].Replace("\\\\","\\"));
//if(fileContents.Contains(""))
if (fileContents.IndexOf(strToSearch) != -1)
{
Console.WriteLine("string contains strsearch");
}
}
}上面的代码依次遍历所有files和读取所有文件的内容,但是,我无法从文件内容中比较/找到确切的字符串。
由于文件内容/新行字符等方面的额外空间。
示例'fileContent‘字符串:
<%@ Page Title="" Language="C#" MasterPageFile="~/_masterpages/MasterPage.master" AutoEventWireup="true" CodeFile="ChangePassword.aspx.cs" Inherits="Account_ChangePassword" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<meta name="description" content="NOINDEX" />
<meta name="keywords" content="NOINDEX" />
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentHeaderNav" Runat="Server">
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentBody" Runat="Server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ContentToggleBox" Runat="Server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="ContentBottom" Runat="Server">
</asp:Content>样本'searchString‘:
<meta name="description" content="NOINDEX" /><meta name="keywords" content="NOINDEX" />有什么建议请.!
帮助感激!
发布于 2016-03-02 05:44:26
可以使用正则表达式搜索替换。
foreach (string item in strFiles)
{
innerList = item.Split(',');
if(!string.IsNullOrEmpty(innerList[0]))
{
fileList.Add(innerList[0]);
fileContents = File.ReadAllText(innerList[0].Replace("\\\\","\\"));
if(Regex.IsMatch(fileContents,@"<meta[^>]*name=""description""[^>]*content=""NOINDEX""[^*]*/>\s*<meta[^>]*name=""keywords""[^>]*content=""NOINDEX""[^*]*/>"))
{ Console.WriteLine("string contains strsearch");
}
}
}
}如果您想替换它,可以使用以下命令:
Regex.Replace(fileContents,@"<meta[^>]*name=""description""[^>]*content=""NOINDEX""[^*]*/>\s*<meta[^>]*name=""keywords""[^>]*content=""NOINDEX""[^*]*/>", ReplacementString)发布于 2016-03-02 05:44:28
不久前,我写了一个小应用程序来查找隐藏在其他英语单词中的英语单词,而如果你从“外部”单词中删除“内部”单词,如果结果仍然是一个英语单词(是的,我有时会感到无聊),我也做了类似的事情。
结果是一个我认为与你相关的小博客,主要是因为我正在加载大量的文件并对它们进行搜索。
正如您所看到的,我确实使用了并行执行来加速这个过程,这给了我不到50 is的结果,这在我的书中是可以接受的:)
希望这能满足你的需要!
https://stackoverflow.com/questions/35739285
复制相似问题