-2

我有一组 40 个字符,它们有自己的代码点。例如,U0678u0679等等。如何根据代码点从文本中检索仅包含这些字符的单词、字符串和子字符串,而忽略所有其他字符?我的旧代码很痛苦

private string token(string x)
{
    Regex exclude = new Regex(@"\d|\s+|/|-|[A-Za-z]", RegexOptions.Compiled);
    return string.Join(" ",
      (from s in Regex.Split(x, "([ \\t{}():;.,!ـ؛،؟ \"\n])")
       where !exclude.IsMatch(s)
       select s).ToArray());
}

已编辑。假设我有字符串“aaa bbb ccc ddd”。然后我想只检索单词 aaa 和 bbb。然后我想做类似的事情

Regex regEx = new Regex(@"\u0041|\u0042");
Match match = regEx.Match(mystring);
if(match.Success)
 then do somthing

但我有 40 个字符。

4

1 回答 1

3

Ok, so you have a set of space delimited strings, and a set of 40 characters. You wish to find which of those strings (separated by spaces) are built up of only combinations of those 40 characters?

@ Chris, yes, exactly .

var charSet = new HashSet<char>("abcde\x015f" + Regex.Unescape("\u0066"));
//or var charSet = new HashSet<char>(new[] { 'a', 'b', 'c', 'd', 'e', 'ş', 'f'});
//or var charSet = new HashSet<char>(new[] { '\x0061', '\x0062', '\x0063', '\x0064', '\x0065', '\x015F', '\x0066'});
//or var charSet = new HashSet<char>(Regex.Unescape("\u0061\u0062\u0063\u0064\u0065\u015F\u0066"));
//or var charSet = new HashSet<char>("\x0061\x0062\x0063\x0064\x0065\x015F\x0066");

string input = "abc  defş aaa xyz";

var words =  input.Split()
                .Where(s => !String.IsNullOrWhiteSpace(s))
                .Where(s => s.All(c => charSet.Contains(c)))
                .ToList();
于 2012-12-16T18:39:50.563 回答