一个正则表达式,永远不会被任何东西匹配 [英] A Regex that will never be matched by anything
问题描述
所以;你的想法是什么 - 正则表达式是什么样的,永远不会与任何字符串相匹配!
编辑:为什么我想要这个?那么,首先因为我觉得有趣的是想到这样的表达,其次是因为我需要一个脚本。
在这个脚本中,我将一个字典定义为字典< string,Regex>
。如您所见,这包含一个字符串和一个表达式。
根据该字典,我创建的方法都使用这个字典作为仅作为他们应该如何工作的参考,其中一个与正则表达式匹配解析的日志文件。
如果一个表达式匹配,另一个 Dictionary< string,long>
添加了表达式返回的值。所以,要捕获字典中表达式不匹配的任何日志消息,我创建了一个名为unknown的新组。
对于这个组, t匹配任何其他添加。但是为了防止未知表达式发生错误(意外)一个日志消息,我不得不创建一个绝对不能匹配的表达式,无论我提供什么字符串。
因此,你有我的理由这个不是真正的问题...
实际上很简单,虽然这取决于实现/标志 *:
$ a
匹配一个字符 a
串。祝你好运。
*)最初我没有多思考多行模式正则表达式,其中 $
也匹配一行的末尾。实际上,它会匹配换行符之前的空字符串,所以一般的字符,如 a
永远不会出现在 $
。
This might sound like a stupid question, but I had a long talk with some of my fellow developers and it sounded like a fun thing to think of.
So; what's your thought - what does a Regex look like, that will never be matched by any string, ever!
Edit: Why I want this? Well, firstly because I find it interesting to think of such an expression and secondly because I need it for a script.
In that script I define a dictionary as Dictionary<string, Regex>
. This contains, as you see, a string and an expression.
Based on that dictionary I create methods that all use this dictionary as only reference on how they should do their work, one of them matches the regexes against a parsed logfile.
If an expression is matched, another Dictionary<string, long>
is added a value that is returned by the expression. So, to catch any log-messages that are not matched by an expression in the dictionary I created a new group called "unknown".
To this group everything that didn't match anything other is added. But to prevent the "unknown"-expression to mismatch (by accident) a log-message, I had to create an expression that is most certainly never matched, no matter what string I give it.
Thus, there you have my reason for this "not a real question"...
This is actually quite simple, although it depends on the implementation / flags*:
$a
Will match a character a
after the end of the string. Good luck.
*) Originally I did not give much thought on multiline-mode regexp, where $
also matches the end of a line. In fact, it would match the empty string right before the newline, so an ordinary character like a
can never appear after $
.
这篇关于一个正则表达式,永远不会被任何东西匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!