gpt4 book ai didi

c# - 检查多个 RegEx 匹配的文本

转载 作者:行者123 更新时间:2023-11-30 22:15:24 24 4
gpt4 key购买 nike

下面的代码读取一些文本(从 OCR 库扫描)并检查文本中的几个简单单词“the”、“date”、“or”、“to”、“and”...... 如果它找到其中一个词,然后该函数返回 true >>> 意味着它刚刚扫描的页面以正确的方式打开。如果该函数返回 false,则页面上下颠倒,并转到旋转页面的函数。

我只是想找出执行此操作的最佳方法。我不是正则表达式大师,但第一个 if 语句返回 true(因此它找到“日期”)。但是,即使我再次寻找“日期”,第二个 if 语句也会返回 false。

条件或 || 不适用于正则表达式吗?

static Boolean CheckIfPDFisTurnedRightWay(List<tessnet2.Word> wordList)
{
if (wordList.Count >= 70)
{
var text = wordList.Select(w => w.Confidence >= 40 ? w.Text : "DONTMATCH").Aggregate((x, y) => x + " " + y);
if (!Regex.IsMatch(text, @"date", RegexOptions.IgnoreCase))
return false;
if (!Regex.IsMatch(text, @"[trf]h[ec]", RegexOptions.IgnoreCase) | !Regex.IsMatch(text, @"date", RegexOptions.IgnoreCase) || !Regex.IsMatch(text, @"[a0o][tfr]", RegexOptions.IgnoreCase) || !Regex.IsMatch(text, @"[ao]nd", RegexOptions.IgnoreCase) || !Regex.IsMatch(text, @"[frt][o0]", RegexOptions.IgnoreCase))
return false;
}
return true;
}

最佳答案

IsMatch 仅返回一个 bool 值,因此您应该能够使用 ||

您可能有错别字。检查前两个 !Regex.IsMatch 语句之间的单管道:

if (!Regex.IsMatch(text, @"[trf]h[ec]", RegexOptions.IgnoreCase)
| !Regex.IsMatch(text, @"date", RegexOptions.IgnoreCase)
|| ...

此外,如果您只想在第二个 if 语句中的所有表达式都不匹配时返回 false,您可能想改用 && 运算符。

if ((text doesn't match 1st expression) and (text doesn't match 2nd expr) and ... )
return false;

关于c# - 检查多个 RegEx 匹配的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17981757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com