您如何“调试"产品? sed的正则表达式? [英] How do you "debug" a regular expression with sed?

查看:63
本文介绍了您如何“调试"产品? sed的正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过sed使用正则表达式.我已经用kiki测试了我的regex,这是一个用于测试regexpd的gnome应用程序,它可以在kiki中正常工作.

I'm trying to use a regexp using sed. I've tested my regex with kiki, a gnome application to test regexpd, and it works in kiki.

date: 2010-10-29 14:46:33 -0200;  author: 00000000000;  state: Exp;  lines: +5 -2;  commitid: bvEcb00aPyqal6Uu;

我什么都不想替换author: 00000000000;.因此,我创建了正则表达式,当我在kiki中对其进行测试时可以正常工作:

I want to replace author: 00000000000; with nothing. So, I created the regexp, that works when I test it in kiki:

author:\s[0-9]{11};

但是当我在sed中对其进行测试时不起作用.

But doesn't work when I test it in sed.

sed -i "s/author:\s[0-9]{11};//g" /tmp/test_regex.txt

我知道正则表达式有不同的实现,这可能是问题所在.我的问题是:如何至少尝试对sed进行调试"?为什么它不起作用?

I know regex have different implementations, and this could be the issue. My question is: how do I at least try do "debug" what's happening with sed? Why is it not working?

推荐答案

我的sed版本不喜欢{11}位.用以下方式处理行:

My version of sed doesn't like the {11} bit. Processing the line with:

sed 's/author: [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9];//g'

工作正常.

我调试的方式正是我在这里所做的.我只是构造了一个命令:

And the way I debug it is exactly what I did here. I just constructed a command:

echo 'X author: 00000000000; X' | sed ...

一次删除一个更高级的正则表达式:

and removed the more advanced regex things one at a time:

  • 使用<space>而不是\s并没有解决问题.
  • 用11个[0-9]副本替换了[0-9]{11}
  • used <space> instead of \s, didn't fix it.
  • replaced [0-9]{11} with 11 copies of [0-9], that worked.

由于我在成功使用sed之前已经使用了正则表达式的所有其他功能,因此必须成为其中之一.

It pretty much had to be one of those since I've used every other feature of your regex before with sed successfully.

但是,实际上,如果没有[0-9]的11个可怕副本,此将会实际上起作用,您只需要转出括号[0-9]\{11\}.我必须承认,我没有去尝试一下,因为它可以使用倍数,而且我通常对sed的简洁性不太担心,因为我倾向于将其更多地用于快速脏工作:-)

But, in fact, this will actually work without the hideous 11 copies of [0-9], you just have to escape the braces [0-9]\{11\}. I have to admit I didn't get around to trying that since it worked okay with the multiples and I generally don't concern myself too much with brevity in sed since I tend to use it more for quick'n'dirty jobs :-)

但是括号方法 更加简洁和适应性强,很高兴知道该怎么做.

But the brace method is a lot more concise and adaptable and it's good to know how to do it.

这篇关于您如何“调试"产品? sed的正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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