0

我是 Perl 的新手...我想用 Perl 读取文件。每次找到两个或多个大写连续单词时,如何使用正则表达式对其进行缩写?例如,

“图形用户界面的前身是由 Douglas Engelbart 领导的斯坦福研究所的研究人员发明的。他们开发了使用鼠标操作在线系统的基于文本的超链接。” 维基

结果:“由 DE 领导的 SRI 的研究人员发明了 GUI 的前身。他们开发了使用鼠标操作 OLS 的基于文本的超链接。”

4

2 回答 2

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 .
于 2013-05-14T01:05:09.030 回答
1
s|\b(?:[A-Z][a-z]+\s+)+(?:[A-Z][a-z]+\b)|$match = $&; $match =~ s/[a-z\s]+//g;$match|ge

e修饰符在替换中执行 perl 语句。

于 2013-05-14T00:58:32.923 回答