SED正则表达式麻烦 [英] SED regular expressions trouble

查看:67
本文介绍了SED正则表达式麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我构建了以下正则表达式以修复带有无效标签的大型 sql 转储此搜索

I have build the following regular expression in order to fix a big sql dump with invalid tags This searches

\[ame=(?:\\"){0,1}(?:http://){0,1}(http://(?:www.|uk.|fr.|il.|hk.){0,1}youtube.com/watch\?v=([^&,",\\]+))[^\]]*\].+?video\]|\[video\](http://(?:www.|uk.|fr.|il.|hk.){0,1}youtube.com/watch\?v=([^\[,&,\\,"]+))\[/video\]

这代替

[video=youtube;$2$4]$1$3[/video]

所以:

[ame=\"http://www.youtube.com/watch?v=FD5ArmOMisM\"]YouTube - Official Install Of X360FDU![/video]

将成为

[video=youtube;FD5ArmOMisM]http://www.youtube.com/watch?v=FD5ArmOMisM[/video]

它在 EditPadPro (Windows) 中的表现就像一个魅力,但当我尝试将它导入基于 Linux 的 MySQL 时,它使我与代码页发生冲突.因此,由于该文件来自 Linux 安装,因此我尝试使用 SED 碰运气,但它给了我错误错误错误.显然它有一种不同的方式来构建正则表达式.

It behaves like a charm in EditPadPro (Windows) but it gives me conflicts with the codepages when I try to import it in my Linux based MySQL. So since the file comes from a Linux installation I tried my luck with SED but it gives me errors errors errors. Obviously it has a different way to build regular expressions.

替换很紧急,所以没时间看 SED 手册.

It is quite urgent to do the substitutions so I have no time reading the SED manual.

您能帮我将我的正则表达式迁移到 SED 友好格式吗?

Can you give a hand to migrate my regular expressions to a SED friendly format?

提前谢谢!

更新:我添加了建议的转义字符

UPDATE: I added the escape chars proposed

\[ame=\(?:\\"\)\{0,1\}\(?:http:\/\/\)\{0,1\}\(http:\/\/\(?:www.|uk.|fr.|il.|hk.\)\{0,1\}youtube.com\/watch\?v=\([^&,",\\]+\))[^\]]*\].+?video\]|\[video\]\(http:\/\/\(?:www.|uk.|fr.|il.|hk.\)\{0,1\}youtube.com\/watch\?v=\([^\[,&,\\,"]+\))\[\/video\]

但我仍然收到错误 - 未知命令:')'

but I still get errors - Unkown command: ')'

推荐答案

您的正则表达式正在使用 PCRE - Perl 兼容正则表达式 - 符号.根据 POSIX 的定义(编纂了大约 1978 年第 7 版 Unix 的标准化内容,这是 Unix 以前版本的延续),sed 不支持 PCRE.

Your regular expressions are using PCRE - Perl Compatible Regular Expression - notations. As defined by POSIX (codifying what was standardized by 7th Edition Unix circa 1978, which was a continuation of the previous versions of Unix), sed does not support PCRE.

即使 GNU sed 4.2.1 版,支持 ERE(扩展正则表达式)以及 BRE(基本正则表达式)不支持 PCRE.

Even GNU sed version 4.2.1, which supports ERE (extended regular expressions) as well as BRE (basic regular expressions) does not support PCRE.

您最好的选择可能是使用 Perl 为您提供所需的 PCRE.如果做不到这一点,请使用您选择的具有 PCRE 支持的脚本语言.

Your best bet is probably to use Perl to provide you with the PCRE you need. Failing that, take the scripting language of your choice with PCRE support.

这篇关于SED正则表达式麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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