c# - 重用正则表达式类以在多个线程中进行匹配
问题描述
此代码在 Web 应用程序环境中是否被视为线程安全?具体IsMobile
功能?是否可以从控制器在高流量 Web 应用程序中调用它。
我在 MSDN 上读到 regex 类是线程安全的,但是从匹配项返回的集合可能会出现问题,所以我不确定这是否适用于我的用例。
static Regex rg = new Regex(@"(?:phone|windows\s+phone|ipod|blackberry|(?:android|bb\d+|meego|silk|googlebot) .+? mobile|palm|windows\s+ce|opera\ mini|avantgo|mobilesafari|docomo|KAIOS)");
private static bool IsMobile(string agent)
{
var matchedAgents = rg.Matches(agent);
if (matchedAgents.Count > 0)
return true;
return false;
}
解决方案
推荐阅读
- javascript - 使用nodeJS将文件添加到同一文件夹
- r - 如何将 TTR::adjRatios() 与 tidyquant 一起使用?
- android - 带 Room db 的数字类型
- android - 通过 RecyclerView 中的改造来获取所有用户
- c - C 上的不同错误、错误 C2275、C2146 等
- q# - Q# 与 LIQUi|>
- python - 使用python复制文件并根据CSV重命名它们
- selenium - 尝试在 selenium 中使用 @FindBy 注释时出错
- java - @SpringBootTest 测试类是否正确?
- c# - C# Web API 可以反序列化包含字典的 URI 中的对象吗