使用 C# 从数据库中驻留的字符串集自动生成正则表达式
我的数据库中有大约 100,000 个字符串,我想知道是否有一种方法可以从这些字符串自动生成正则表达式模式。它们都是字母字符串,并使用英文字母的字母集。例如,不使用 (X,W,V)。有没有任何函数或库可以帮助我在 C# 中实现这个目标?示例字符串为
KHTK
RAZ
给定这两个字符串,我的目标是生成一个正则表达式,该正则表达式允许像 (k, kh, kht,khtk, r, ra, raz) 这样的模式当然不区分大小写。我已经下载并使用了一些有助于生成正则表达式的 C# 应用程序,但这在我的场景中没有用,因为我想要一个过程,在该过程中我顺序从数据库读取字符串并向正则表达式添加规则,以便稍后可以在应用程序中重用该正则表达式或保存在磁盘上。
我是正则表达式模式的新手,不知道我要问的事情是否可能。如果不可能,请建议我一些替代方法。
I have about 100,000 strings in database and I want to if there is a way to automatically generate regex pattern from these strings. All of them are alphabetic strings and use set of alphabets from English letters. (X,W,V) is not used for example. Is there any function or library that can help me achieve this target in C#? Example strings are
KHTK
RAZ
Given these two strings my target is to generate a regex that allows patterns like (k, kh, kht,khtk, r, ra, raz) case insensitive of course. I have downloaded and used some C# applications that help in generating regex but that is not useful in my scenario because I want a process in which I sequentially read strings from db and add rules to regex so this regex could be reused later in the application or saved on the disk.
I'm new to regex patterns and don't know if the thing I'm asking is even possible or not. If it is not possible please suggest me some alternate approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种简单(有些人可能会说天真的)方法是创建一个正则表达式模式,该模式连接所有搜索字符串,并用交替运算符
|
分隔:KHTK |RAZ
。K|KH|KHT|KHTK|R|RA|RAZ
。^K$|^KH$|^KHT$|^KHTK$|^R$|^RA$|^RAZ$
我们希望 Regex 类实现能够完成转换 long 的繁重工作。正则表达式模式字符串到高效的匹配器。
这里的示例程序生成 10,000 个随机字符串,以及一个与这些字符串及其所有前缀完全匹配的正则表达式。然后,程序验证正则表达式是否确实与这些字符串匹配,并计算这一切需要多长时间。
在 Intel Core2 盒子上,我得到了 10,000 个字符串的以下数字:
当将字符串数量增加 10 倍(至 100,000)时,我得到:
这是更高的,但增长不是线性的。
该应用程序的内存消耗(在 10,000 个字符串时)开始为 ~9MB,峰值为 ~23MB,其中必须包含正则表达式和字符串集,最后下降到 ~16MB(垃圾收集启动?)该程序没有针对从其他数据结构中剔除正则表达式内存消耗进行优化。
A simple (some might say naive) approach would be to create a regex pattern that concatenates all the search strings, separated by the alternation operator
|
:KHTK|RAZ
.K|KH|KHT|KHTK|R|RA|RAZ
.^K$|^KH$|^KHT$|^KHTK$|^R$|^RA$|^RAZ$
We would expect the Regex class implementation to do the heavy lifting of converting the long regex pattern string to an efficient matcher.
The sample program here generates 10,000 random strings, and a regular expression that matches exactly those strings and all their prefixes. The program then verifies that the regex indeed matches just those strings, and times how long it all takes.
On an Intel Core2 box I'm getting the following numbers for 10,000 strings:
When increasing the number of strings 10-fold (to 100,000), I'm getting:
This is higher, but the growth is less than linear.
The app's memory consumption (at 10,000 strings) started at ~9MB, peaked at ~23MB that must have included both the regex and the string set, and dropped to ~16MB towards the end (garbage collection kicked in?) Draw your own conclusions from that -- the program doesn't optimize for teasing out the regex memory consumption from the other data structures.