c# - 正则表达式匹配空单元格
问题描述
我尝试在我的函数中使用正则表达式。它允许在正则表达式中找到具有指定字符串的单元格(忽略大小写)。
问题是,如果我在单元格之前有空框,我有一个例外。我不明白为什么 NULL 单元格是个问题。
我的功能:
public int FindSearchableCol()
{
var j = 1;
var x = FindMainInfoStartRow();
var i = x;
string test = ws.Cells[i, j].Value2;
Regex searchable = new Regex(@"(?<=\*)(searchable)(?=\*)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
try
{
while (true)
{
MatchCollection matches = searchable.Matches(ws.Cells[i, j].Value2);
if (matches.Count > 0)
{
return j;
}
else
{
i++;
if (ws.Cells[i, j].Value2 == "*MAIN INFO END*")
{
i = x;
j++;
}
}
}
}
finally
{
}
}
解决方案
利用 :
MatchCollection matches = searchable.Matches(ws.Cells[i, j].Value2 ?? string.Empty)
反而 :
MatchCollection matches = searchable.Matches(ws.Cells[i, j].Value2)
推荐阅读
- c# - 如何将验证错误传递给可重用用户控件中的子控件
- google-cloud-platform - 将谷歌端点中的路径参数传递给后端不起作用
- java - 自定义treeSet类,迭代器怎么写
- r - 使用 apply 时参数不一致
- reactjs - 使用 React 钩子取消 HTTP 获取请求
- java - 在 Windows、Linux 和 Mac 中是否有特定于操作系统的 API 用于调用文件选择器对话框?
- hive - HBase 存储处理程序:UnknownProtocolException:未找到名称 AuthenticationService hbase:meta 的协处理器
- groff - 在命令行禁用 groff 断字
- javascript - 在 JavaScript 中解析 Promise
- plsql - Oracle APEX 在 PL/SQL 中调用 Dialogflow V2 - 使用 OAuth2 或 Google 服务帐户进行身份验证