Chinaunix

标题: 编程练习(There's more than one way to do it!) [打印本页]

作者: xiumu2280    时间: 2016-10-17 16:37
标题: 编程练习(There's more than one way to do it!)
一个字符串
  1. my $line = "adasd+dad+dada+dad+dada";
复制代码

将第二和第四个 '+' 号变成 '+\\'

只能用perl,  实现方法不限。

作者: sunzhiguolu    时间: 2016-10-17 17:03
如果字符串中 "+" 数量有规律的话,
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;

  4. my $str = 'adasd+dad+dada+dad+dada';
  5. $str =~ s/(\+.*?\+)/$1\\\\/g;
  6. print $str;
复制代码

作者: sunzhiguolu    时间: 2016-10-17 17:18
如果原始字符串中 "+" 字符数量不确定, 添加一个计数器 只在计数器的值为 2 OR 4 的时候打印 \\ 字符.




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2