有没有比正则表达式更快的单词匹配整个单词?

编程入门 行业动态 更新时间:2024-10-11 17:24:51
本文介绍了有没有比正则表达式更快的单词匹配整个单词?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

编辑:

我最初的问题是询问匹配整个单词的速度是否比正则表达式还要快.我添加了我的代码,并运行了一些测试.详细信息如下

My original question was asking whether anything could ever be faster than regex for matching a whole word. I have added my code, and have run several tests. The details are below

我的示例匹配字符串(来自老人与海)

My sample matching string (from The Old Man And The Sea)

他是一个老人,独自一人在墨西哥湾流中的一条小艇上钓鱼,现在他已经八十四天没有钓鱼了.在最初的四十天里,有一个男孩和他在一起.但是在四十天没吃鱼之后,男孩的父母告诉他,现在肯定是老人了,最后是萨拉sal,这是最不幸的一种形式,男孩顺着他们的命令去了另一条船,第一条船抓了三条好鱼.周

这是我的正则表达式

"(\b(cod|tuna|mackerel|plaice|haddock|salmon|prawns|shrimp|fishcake|halibut|sole|eel|anchovy|anchovies|sardine|herring|bonito|whiting|seabass|carp|crab|flounder|pollock|mullet|ray|ray wings|clam|mussel|scallop)(s?)\b)"

这是我第一次不使用正则表达式的匹配尝试

Here's my first matching attempt without regex

public static words = "cod|tuna|mackerel|plaice|haddock|salmon|prawns|shrimp|fishcake|halibut|sole|eel|anchovy|anchovies|sardine|herring|bonito|whiting|seabass|carp|crab|flounder|pollock|mullet|ray|ray wings|clam|mussel|scallop"; public static bool MatchBySplitting(string sentence) { string[] sentence_words = sentence.Split(',','.',' ',';','-'); string[] match_words = words.Split('|'); foreach(string w in sentence_words) { foreach(string m in match_words) { if(m == w) return true; } } return false; }

每次运行5000次迭代:

Running 5000 iterations of each:

  • 正则表达式匹配: 250-300毫秒
  • MatchBySplitting :250-350毫秒,与正则表达式可比.
  • Regex Matching: 250-300 ms
  • MatchBySplitting: 250-350 ms, a comparable time to the regex.

但是,如果我将匹配的字符串缩短到第一行,我的结果就会改变

However, if I shorten my matching string to just the first line, my results change

他是一个老人,独自一人在墨西哥湾流的一条小艇中捕鱼,现在已经走了八十四天了,没有捞到鱼.

regex保持不变,但是MatchBySplitting加快了很多速度:

The regex stays about the same, but MatchBySplitting speeds up a lot:

  • 正则表达式匹配:220-260毫秒
  • MatchBySplitting :50-150毫秒-比正则表达式快.
  • Regex Matching: 220-260 ms
  • MatchBySplitting: 50-150 ms - Faster than regex.

如果我随后开始混淆经典,并插入一个将匹配的单词

If I then start messing with the classics, and insert a word that will match

他是一个老人,独自一人在墨西哥湾流中的一条小艇上钓鱼,现在他已经八十四天没有钓鱼了.然后在第五八十天,他抓到了一条金枪鱼.结束

  • 正则表达式匹配:170-300毫秒
  • MatchBySplitting :100-200毫秒-比正则表达式快.
  • Regex Matching: 170-300 ms
  • MatchBySplitting: 100-200 ms - Faster than regex.

我想我已经在这里回答了我自己的问题.在大多数情况下,我的自定义匹配方法似乎等于或快于正则表达式.

I think I've answered my own question here. My custom matching method seems to be equal to or faster than regex in most cases.

但是,我没有在代码中涵盖所有单词边界(!?),因此如果在其中添加这些单词,可能会稍微放慢速度.

However, I haven't covered all word boundaries in my code (!?) so it may slow down a little if I add those in.

推荐答案

尝试制作一个已编译的正则表达式,如下所示:

Try making a compiled regex, like this:

static readonly Regex CornRegex = new Regex("\b(corn)\b", RegexOptions.Compiled);

这实际上将生成并编译一个方法,该方法包含匹配该正则表达式所需的汇编指令.它应该非常快,可以与编写自己的遍历各个字符的自定义函数相媲美.

This will actually generate and compile a method that contains the assembly instructions needed to match that regex. It should be very fast, comparable to writing your own custom function that loops over the individual characters.

更多推荐

有没有比正则表达式更快的单词匹配整个单词?

本文发布于:2023-11-02 12:08:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1552357.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   更快   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!