C# 正则表达式如何查找;不连续后缀";后缀
我的文档中有许多文本行,混合了两种语言,如下所示:(看单词עשמ和טקסט) 这是该方法的完整代码:C# 正则表达式如何查找;不连续后缀";后缀,c#,regex,C#,Regex,我的文档中有许多文本行,混合了两种语言,如下所示:(看单词עשמ和טקסט) 这是该方法的完整代码: string[] files = Directory.GetFiles(@"C:\Test", "*.html", SearchOption.AllDirectories); foreach (string file in files) { string fileContent = File.ReadAllText(file, Encoding.GetEncoding(1255));
string[] files = Directory.GetFiles(@"C:\Test", "*.html", SearchOption.AllDirectories);
foreach (string file in files)
{
string fileContent = File.ReadAllText(file, Encoding.GetEncoding(1255));
fileContent = fileContent.Replace("windows-1255", "utf-8");
Regex hrefRegex = new Regex("((href=\"http://.+?sQuery=[^\"]*)([א-ת]+)([^\"]*\"))+?");
fileContent = Regex.Replace(fileContent,hrefRegex.ToString(), delegate(Match match)
{
string textToEncode = match.Groups[3].Value;
string encodedText = HttpUtility.UrlEncode(textToEncode, new UTF8 Encoding(false)).ToUpper();
return match.Groups[2].Value + encodedText + match.Groups[4].Value;
});
File.WriteAllText(file + "_fix.html", fileContent, new UTF8Encoding(false));
}
我做错了什么
我如何更新我的正则表达式模式,以便它在href中找到所有“其他语言”部分,因为现在我只带了第一个部分。它只有一个匹配项,即整个字符串。如果你想逐字翻译,你必须使用这个正则表达式:
([א-ת])
,如果你想翻译每个单词,请使用这个正则表达式:([א-ת]+)
编辑:要仅翻译href部分中的字符,请执行以下操作:
fileContent = Regex.Replace(fileContent, hrefRegex , delegate(Match match)
{
string textToEncode = match.ToString();
textToEncode = Regex.Replace(textToEncode, "[א-ת]", delegate(Match smallMatch)
{
return HttpUtility.UrlEncode(smallMatch.ToString(), new UTF8 Encoding(false)).ToUpper();
});
return textToEncode;
});
你想实现什么?我有一个问题,与不同类型的浏览器链接,因为其他语言和每个浏览器生成的请求。它解码为另一个编码,我无法处理文本。您可以对表单数据使用
POST
,而不是使用GET
并在URL
中传递参数,我知道。但我不能,因为我需要保存html的原始结构。这些文件来自我正在使用的图书馆。我认为搜索引擎不会点击输入[submit]元素,所以我会放弃搜索引擎优化。我只想翻译href文本中的文本。不是所有的医生。
string[] files = Directory.GetFiles(@"C:\Test", "*.html", SearchOption.AllDirectories);
foreach (string file in files)
{
string fileContent = File.ReadAllText(file, Encoding.GetEncoding(1255));
fileContent = fileContent.Replace("windows-1255", "utf-8");
Regex hrefRegex = new Regex("((href=\"http://.+?sQuery=[^\"]*)([א-ת]+)([^\"]*\"))+?");
fileContent = Regex.Replace(fileContent,hrefRegex.ToString(), delegate(Match match)
{
string textToEncode = match.Groups[3].Value;
string encodedText = HttpUtility.UrlEncode(textToEncode, new UTF8 Encoding(false)).ToUpper();
return match.Groups[2].Value + encodedText + match.Groups[4].Value;
});
File.WriteAllText(file + "_fix.html", fileContent, new UTF8Encoding(false));
}
fileContent = Regex.Replace(fileContent, hrefRegex , delegate(Match match)
{
string textToEncode = match.ToString();
textToEncode = Regex.Replace(textToEncode, "[א-ת]", delegate(Match smallMatch)
{
return HttpUtility.UrlEncode(smallMatch.ToString(), new UTF8 Encoding(false)).ToUpper();
});
return textToEncode;
});