- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在寻找一种解决方案来将单个字符串与一组通配符字符串进行匹配。例如
>>> match("ab", ["a*", "b*", "*", "c", "*b"])
["a*", "*", "*b"]
输出的顺序并不重要。
我将按照 10^4 个通配符字符串的顺序进行匹配,我将执行大约 ~10^9 个匹配调用。这意味着我可能不得不像这样重写我的代码:
>>> matcher = prepare(["a*", "b*", "*", "c", "*b"]
>>> for line in lines: yield matcher.match("ab")
["a*", "*", "*b"]
我已经开始用 Python 编写一个 trie 实现来处理通配符,我只需要正确处理这些极端情况。尽管如此,我还是很想听听; 你会如何解决这个问题?是否有任何 Python 库可以让我更快地解决这个问题?
到目前为止的一些见解:
最佳答案
您可以使用 re2 library 中的 FilteredRE2
类在 Aho-Corasick 算法实现(或类似算法)的帮助下。来自 re2 docs :
Required substrings. Suppose you have an efficient way to check which of a list of strings appear as substrings in a large text (for example, maybe you implemented the Aho-Corasick algorithm), but now your users want to be able to do regular expression searches efficiently too. Regular expressions often have large literal strings in them; if those could be identified, they could be fed into the string searcher, and then the results of the string searcher could be used to filter the set of regular expression searches that are necessary. The FilteredRE2 class implements this analysis. Given a list of regular expressions, it walks the regular expressions to compute a boolean expression involving literal strings and then returns the list of strings. For example, FilteredRE2 converts (hello|hi)world[a-z]+foo into the boolean expression “(helloworld OR hiworld) AND foo” and returns those three strings. Given multiple regular expressions, FilteredRE2 converts each into a boolean expression and returns all the strings involved. Then, after being told which of the strings are present, FilteredRE2 can evaluate each expression to identify the set of regular expressions that could possibly be present. This filtering can reduce the number of actual regular expression searches significantly.
The feasibility of these analyses depends crucially on the simplicity of their input. The first uses the DFA form, while the second uses the parsed regular expression (Regexp*). These kind of analyses would be more complicated (maybe even impossible) if RE2 allowed non-regular features in its regular expressions.
关于python - 如何有效地将字符串与一组通配符字符串进行匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12904860/
我的网址看起来像 '/api/comments/languages/124/component/segment_translation/2' 我知道 url 的哪些部分是静态的;并且是动态的 - 并且
如何使用通配符查找和替换主域之后的所有字符(包括“/”字符)? 例如,我有以下 4 行: intersport-schaeftlmaier.de/ weymouthhondapowersports.c
我有 3 个控件,其 ID 为 control_1、control_2、control_3。 我想隐藏这些控件。 目前我正在使用这个: $('#control_1').hide(); $('#cont
我有一个旧歌曲数据库,我想将其转移到新数据库。我的旧数据库看起来像这样,多个值被填充在一个用逗号分隔的字段中 SONG id | title | artist |
首先,我知道downloads表没有标准化。 我有这两个表: downloads map | author 1 | Nikola 2 | Nikola George 和 mappers mapper_
通配符可用于替代字符串中的任何其他字符。 SQL 通配符 在 SQL 中,通配符与 SQL LIKE 操作符一起使用。 SQL 通配符用于搜索表中的数据。 在 SQL 中,可使用以下通配符:
我在 shell 脚本中有一行看起来像这样: java -jar "$dir/"*.jar ,因为我只想执行该文件夹中恰好命名的 jar 文件。但这并不像我预期的那样有效。我收到错误消息: Error
我想在 Active Directory 用户的所有属性中搜索特定电话号码/分机号。 我可以像这样获取所有属性: get-aduser joesmith -Properties * 但我想过滤结果,例
我在运行 Python 3在 Windows 机器上使用 PowerShell .我正在尝试执行一个 Python 文件,然后使用通配符将多个文件(file1.html、file2.html 等)作为
我有一个 div,并且有一些处于未定义级别的子节点。 现在我必须将每个元素的 ID 更改为一个 div。如何实现? 我想,因为它们有向上的ID,所以如果父级是id='path_test_maindiv
我是 Lua 的新手,所以我现在正在学习运算符部分。在 Lua 中是否有与字符串一起使用的通配符? 我有 PHP 背景,我实际上是在尝试编写以下代码: --scan the directory's f
我在 countList 方法上遇到编译时错误。 public static void countList( List list, int count ){ for( int i =
我们需要在运行时检索多个类实例,而无需手动维护所有可用类型的列表。 可能的方法: 检索带有@xy注释的每种类型的实例 检索每种类型的实例实现接口(interface)iXY 检索每种类型的实例,命名如
我目前陷入了序言问题。 到目前为止我有: film(Title) :- movie(Title,_,_).(其中“movie(T,_,_,)”是对我的引用数据库) namesearch(Title,
我想从字符表达式(在 R 中)中删除一个“*”。在阅读帮助页面并尝试谷歌后,我无法充分理解 gsub 的复杂性。有人可以建议我该怎么做吗? 谢谢, 乔纳森。 最佳答案 您需要转义两次:一次针对 R,一
在我的 DOM 中,我有一个动态生成对话框的表。 DOM 中的对话框将具有以下形式的 ID: id="page:form:0:dlg" id="page:form:1:dlg" id="page:fo
我是 Java 新手,并且已经陷入这样一种情况,很明显我误解了它如何处理泛型,但是阅读教程和搜索 stackoverflow 并没有(至少到目前为止)让我清楚我怀疑我滥用了通配符。需要注意的是,我有
我想使用 jQuery 更改单击时图像的 src 属性。这是 HTML: View 2 在 img src 中,我想将“a”替换为“b”,但我的问题是我想忽略它前面的“1”,因为它也可能看起来像这样
我有一个 mysql 数据库,我的表是: Name | passcode ---------------------- hi* | 1111 ------------------
我想选择所有在星号所在位置具有确切 4 个“未知”字符的文档:(例如“****”可能是“2018”) foreach (string s in Directory.GetFiles(@"C:\User
我是一名优秀的程序员,十分优秀!