如何使用Regex从html文件中获取希伯来文字符串/单词

本文关键字:文字 字符串 单词 获取 Regex 何使用 html 文件 | 更新日期: 2023-09-27 18:20:14

在Form1的顶部,我做了:

private static readonly Regex AnyWordRegex = new Regex(@"((?<word>[a-zA-Z]{4,}))", RegexOptions.Singleline | RegexOptions.Compiled);

在构造函数中,我做了:

string file = File.ReadAllText(OriginalHtmlFilePath);
string strippedHtml = StripHtml(file);

在这种情况下,OriginalHtmlFilePath包括带有希伯来语单词的html文件。

这是StripHtml:

public static string StripHtml(string htmlString)
        {
            return StripHtmlRegex.Replace(htmlString, @"|");
        }

在它之后,我看到strippedHtml包含希伯来语单词。然后我在构造函数中做:

_words = ExtractWords(strippedHtml);

_单词是列表

private static List<string> ExtractWords(string text)
        {
            MatchCollection matchCollection = AnyWordRegex.Matches(text);
            return (from Match match in matchCollection select match.Groups[1].Value).ToList();
        }

完成ExtractWords后,我发现List _words只包含英语单词。大约608个单词只有英语。但在这种情况下,我正在开发的网站是www.walla.co.il或www.ynet.co.il,这是一个希伯来语网站。

如果我在cnn.com或foxnews.com的任何英文网站上工作,一切都很好。

如何使用Regex从html文件中获取希伯来文字符串/单词

您可以使用'p{L}而不是[a-zA-Z]来匹配所有字母表中的所有字母,或者更具体地使用['p{IsHebrew}a-zA-Z]