简体   繁体   English

使用Perl仅替换字符串的一部分

[英]Substitute only one part of a string using perl

I have an array that have some symbols that I want to remove and even thought I find a solution, I will like to know if this is the right way because I'm afraid if I use it with array will remove the character that I might need on future arrays. 我有一个数组,其中有一些要删除的符号,甚至以为我找到了解决方案,我想知道这是否是正确的方法,因为我担心如果将其与数组一起使用会删除我可能会删除的字符需要将来的阵列。

Here is an example item on my array: 这是我的阵列上的一个示例项目:

$string1='22      |      logging monitor informational';

so I try the following: 所以我尝试以下方法:

$string1=~ s/\s{6}\|(?=\s{6})//;

So my output is: 所以我的输出是:

22      logging monitor informational

Is the other way that best match "|". 是与“ |”最匹配的另一种方式。 I just want to remove the pipe character. 我只想删除管道字符。

Thanks in advance 提前致谢

"I want to remove just the pipe character." “我只想删除管道字符。”

OK, then do this: OK,然后执行以下操作:

$string1 =~ s/\|//;

This will remove the first pipe character in the string. 这将删除字符串中的第一个竖线字符。 (You said in another comment that you don't want to remove any additional pipe characters.) If that's not what you want, then I'd suggest telling us exactly what you do want. (您在另一条评论中说,您不想删除任何其他的竖线字符。)如果这不是您想要的,那么我建议您告诉我们您到底想要什么 We can't read minds, you know. 你知道我们看不懂思想。

In the mean time, I'd also strongly recommend reading the Perl regular expressions tutorial . 同时,我也强烈建议您阅读Perl正则表达式教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM