标题: [问题求助] 字符串替换的代码用perl如何实现 [打印本页]
作者: netdzb 时间: 2020-4-19 18:56 标题: 字符串替换的代码用perl如何实现
- text = '''00:00:23.844 ---> 00:00:26.334
- 00:00:26.444 ---> 00:00:29.614
- 00:00:34.424 ---> 00:00:39.372
- 00:00:44.216 ---> 00:00:46.446
- 00:00:47.151 ---> 00:00:50.556
- 00:00:50.672 ---> 00:00:53.348
- 00:00:54.022 ---> 00:00:55.881
- 00:00:56.045 ---> 00:00:58.634
- 00:00:58.748 ---> 00:01:01.855'''
- timeline = text.split('\n')
-
- for t in timeline:
- newstr = t.replace('.',',')
- print newstr
复制代码
我用python替换成功了,这个perl的脚本也是可以实现的。
思路是逐行读入文本内容,判断行内是否存在 ---> ,
存在的话,就把行内的.替换成,
不存在这个 ---> 标志的话,就跳过这行继续往下读。
作者: 523066680 时间: 2021-4-16 23:40
"判断行内是否存在 --->"
也没看到python脚本里面有做这个判断呀,如果不做判断就全局替换呗
s/./,/g;
作者: 523066680 时间: 2021-4-16 23:46
- use Modern::Perl;
- my $text = <<TEXT;
- 00:00:23.844 ---> 00:00:26.334
- 00:00:26.444 ---> 00:00:29.614
- 00:00:34.424 ---> 00:00:39.372
- 00:00:44.216 ---> 00:00:46.446
- 00:00:47.151 ---> 00:00:50.556
- 00:00:50.672 ---> 00:00:53.348
- 00:00:54.022 ---> 00:00:55.881
- 00:00:56.045 ---> 00:00:58.634
- 00:00:58.748 ---> 00:01:01.855
- something. -- else.
- TEXT
-
- grep {
- s/\./,/g if /--->/;
- say $_;
- } split( "\n", $text );
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |