开发者

How can I strip escape codes from a string in Perl?

开发者 https://www.devze.com 2023-01-07 21:58 出处:网络
The original string is passed as: FileSystems\\/\\1K-blocks=5036316, FileSystems\\/\\开发者_StackOverflow社区Available=3295944, FileSystems\\/\\Filesystem=/dev/cciss/c0d0p2, FileSystems\\/\\Use%=32%

The original string is passed as:

FileSystems\/\1K-blocks=5036316, FileSystems\/\开发者_StackOverflow社区Available=3295944, FileSystems\/\Filesystem=/dev/cciss/c0d0p2, FileSystems\/\Use%=32%

What I need as an output is:

/ 1K-blocks=5036316, / Available=3295944, / Filesystem=/dev/cciss/c0d0p2, / Use%=32%

but trying to delete the '\' with a regex is giving me all kinds of headaches. I keep ending up with:

Fi eSys ems/☺K-b o ks=5036316  Fi eSys ems/Avai ab e=3295944  Fi eSys ems/Fi esy
s em=/dev/  iss/ 0d0p2  Fi eSys ems/SE%=32%  

Perl is apparently seeing it a a control or escape code and really screwing it up.

Any suggestions would be greatly appreciated.


I would use different delimiters (other than /) to make the regex a little easier on the eyes:

s!FileSystems\\/\\!/ !g


These two lines should do it:

s/FileSystems\\//g;
s/\\/ /g;


Using diff. delimiters will make this much less confusing.

$line =~ s!FileSystem\\/\\!/\s!g

would do the trick

0

精彩评论

暂无评论...
验证码 换一张
取 消