我是Perl的新手...... 我想在Perl中读取一个文件。每次我找到两个或多个大写的连续单词时,如何使用正则表达式缩写它们?例如,
“图形用户界面的前身是由斯坦福研究所的研究人员发明的,由Douglas Engelbart领导。他们开发了使用鼠标操作的基于文本的超链接用于在线系统。”维基
结果: “GUI的前身是由DE领导的SRI的研究人员发明的。他们开发了使用鼠标操纵OLS的基于文本的超链接。”
答案 0 :(得分:3)
也可以使用如下表达式在单个传递中完成:
s/\b([A-Z])[a-z]+(?=\s+[A-Z][a-z])|\G(?!^)\s+([A-Z])[a-z]+/$1$2/g;
示例:
$_ = "A precursor to Graphical User Interface was invented by researchers at the Stanford Research Institute, led by Douglas Engelbart. They developed the use of text-based hyperlinks manipulated with a mouse for the On Line System .";
s/\b([A-Z])[a-z]+(?=\s+[A-Z][a-z])|\G(?!^)\s+([A-Z])[a-z]+/$1$2/g;
print;
输出:
A precursor to GUI was invented by researchers at the SRI, led by DE. They developed the use of text-based hyperlinks manipulated with a mouse for the OLS .
答案 1 :(得分:1)
s|\b(?:[A-Z][a-z]+\s+)+(?:[A-Z][a-z]+\b)|$match = $&; $match =~ s/[a-z\s]+//g;$match|ge
e
修饰符在替换中执行perl语句。