Perl 正则表达式去除双制表符、换行符、空格 [英] Perl Regular expression remove double tabs, line breaks, white spaces

查看:128
本文介绍了Perl 正则表达式去除双制表符、换行符、空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个删除双制表符、换行符和空格的 perl 脚本.

I want to write a perl script that removes double tabs, line breaks and white spaces.

到目前为止我所拥有的是:

What I have so far is:

$txt=~s/\r//gs;
$txt=~s/ +/ /gs;
$txt=~s/\t+/\t/gs;
$txt=~s/[\t\n]*\n/\n/gs;
$txt=~s/\n+/\n/gs;

但是,1.不漂亮.应该可以用更少的正则表达式来做到这一点.2.它只是不起作用,我真的不知道为什么.它留下了一些双制表符、空格和空行(即只有制表符或空格的行)

But, 1. It's not beautiful. Should be possible to do that with far less regexps. 2. It just doesn't work and I really do not know why. It leaves some double tabs, white spaces and empty lines (i.e. lines with only a tab or whitespace)

我可以用一段时间来解决它,但这非常缓慢和丑陋.

I could solve it with a while, but that is very slow and ugly.

有什么建议吗?

推荐答案

您的内容有些混杂,并非所有内容都与您所说的相符.让我们分解你拥有的东西,然后也许你可以从那里工作到你想要的.

You've got a bit of a mish-mash of stuff in there, not all of which corresponds to what you said. Let's break down what you have and then perhaps you can work from there to what you want.

$txt=~s/\r//s; # removes a single \r from the line. Did you mean to use g on this one?
$txt=~s/[\t ]\n//s; # match a single \t OR space right before a \n, and remove. 
$txt=~s/ +/ /gs;# match at least 2 spaces, replace with a single space
$txt=~s/\t+/ /gs;# match at least 2 \t, replace with a single space
$txt=~s/\n /\n/s;# remove a space immediately following a \n
$txt=~s/\t /\t/s;# remove a space immediately following a \t
$txt=~s/\n+/ /gs;# match at least 2 \n, replace them all with a single space

我感觉这根本不是你想要完成的.

I have the feeling that's not at all what you want to accomplish.

老实说,我不清楚你想做什么.我阅读您陈述的意图的方式,我以为您想用单标签替换所有双标签,用单换行替换所有双换行符,用单空格替换所有双空格.我会进一步推测,您想要实际运行这些字符,而不仅仅是双打.这是我刚才所说的正则表达式,希望这会给你一些东西:(我也删除了所有 \r).

I'm honestly unclear on what you want to do. The way I read your stated intent, I would have thought you'd want to replace all double tabs with single tabs, all double line breaks with single line breaks, and all double spaces with single spaces. I'll further surmise that you want to actually do runs of those characters, not just doubles. Here's the regexes for what I've just said, hopefully that will give you something to go on: (I've also removed all \r).

$txt=~s/\r//gs;# remove all \r
$txt=~s/\t+/\t/gs;# replace all runs of > 1 tab with a single tab
$txt=~s/\n+/\n/gs;# replace all runs of > 1 \n with a single \n
$txt=~s/ +/ /gs;# replace all runs of > 1 space with a single space

鉴于您尝试使用的正则表达式似乎与我阅读您陈述的愿望的方式不符,我怀疑您在这里真正想做的事情有些模糊.您可能需要进一步考虑您要完成的任务,这应该有助于使正则表达式更加清晰.

Given that your attempted regexes don't seem to match the way I read your stated desire, I suspect that there's some fuzziness about what you really want to do here. You might want to think further about what you are trying to accomplish, which should help the regexes become clearer.

这篇关于Perl 正则表达式去除双制表符、换行符、空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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