如何将PCRE转换为POSIX RE? [英] How to convert a PCRE to a POSIX RE?

查看:74
本文介绍了如何将PCRE转换为POSIX RE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个有趣的问题用于匹配除特定给定字符串之外的任何内容(包括空字符串)的正则表达式都涉及如何进行否定运算在MySQL中超前.海报想要获得效果

This interesting question Regex to match anything (including the empty string) except a specific given string concerned how to do a negative look-ahead in MySQL. The poster wanted to get the effect of

Kansas(?! State)

由于MySQL并未实现前瞻性断言,因此出现了许多答案.

because MySQL doesn't implement look-ahead assertions, a number of answers came up the equivalent

Kansas($|[^ ]| ($|[^S])| S($|[^t])| St($|[^a])| Sta($|[^t])| Stat($|[^e]))

张贴者指出,这是可能会表达很多表情的PITA.

The poster pointed out that's a PITA to do for potentially lots of expressions.

是否存在PCRE(或其他软件包)的脚本/实用程序/模式,该脚本/实用程序/模式会将PCRE(如果可能)转换为不使用Perl的时髦功能的等效正则表达式?我完全知道,某些Perl风格的正则表达式不能表示为普通正则表达式,因此,我当然不希望该工具能够完成不可能的事情!

Is there a script/utility/mode of PCRE (or some other package) that will convert a PCRE (if possible) to an equivalent regex that doesn't use Perl's snazzy features? I'm fully aware that some Perl-style regexes cannot be stated as an ordinary regex, so I would not expect the tool to do the impossible, of course!

推荐答案

您不想这样做.实际上,将高级功能转换为基本功能并不令人难以置信-这只是编译器的另一种风味,并且编译器编写者非常聪明-但是,这些令人眼花features乱的功能解决的大多数事情(a)不可能与标准正则表达式,因为它们可以识别非正则语言,因此您必须对其进行近似处理,以便至少它们适用于有限长度的文本或(b)可能,但只能使用指数大小的正则表达式. 指数"是不要去那里"的代名词.如果您尝试对实际要处理的任何事物使用指数解决方案,则会 陷入OutOfMemory错误和看似无限的循环中.

You don't want to do this. It isn't actually mindbogglingly difficult to translate the advanced features to basic features - it's just another flavor of compiler, and compiler writers are pretty clever people - but most of the things that the snazzy features solve are (a) impossible to do with a standard regex because they recognize non-regular languages, so you'd have to approximate them so that at least they work for a limited-length text or (b) possible, but only with a regex of exponential size. And 'exponential' is compsci-speak for "don't go there". You will get swamped in OutOfMemory errors and seemingly-infinite loops if you try to use an exponential solution on anything you would actually want to process.

换句话说,放弃所有希望,进入这里的人.让正则表达式执行其擅长的工作并使用其他工具完成其余工作实际上总是更好.甚至将正则表达式求逆之类的简单事情,与原始正则表达式结合否定运算符相比,要比由精确的正则表达式求逆器产生的怪异现象要容易得多.

In other words, Abandon all hope, ye who enter here. It is virtually always better to let the regex do what it's good at and do the rest with other tools. Even such a simple thing as inverting a regex is much, much easier solved with the original regex in combination with the negation operator than with the monstrosity that would result from an accurate regex inverter.

这篇关于如何将PCRE转换为POSIX RE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆