Generate an abbreviation from a string in perl using regular expressions

I'm new to Perl... I want to read a file in Perl. Every time I find two or more capitalized consecutive words, how can I abbreviate them using regular expression? For example,

" 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 ." wiki

the result : " 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 ."

Answers


Could also be done in a single pass with an expression like:

s/\b([A-Z])[a-z]+(?=\s+[A-Z][a-z])|\G(?!^)\s+([A-Z])[a-z]+/$1$2/g;

Example:

$_ = "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;

Output:

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 .

s|\b(?:[A-Z][a-z]+\s+)+(?:[A-Z][a-z]+\b)|$match = $&; $match =~ s/[a-z\s]+//g;$match|ge

The e modifier executes perl statements in the replacement.


Need Your Help

Can an `<iframe>` hold multiple pages inside of it?

php html wordpress

I am creating a Wordpress plugin that if possible, I would like to link to a series of pages to an outside website (an affiliate login), instead of re-building a replica of the same pages inside of...

LINQ Inner-Join vs Left-Join

c# .net linq left-join

Using extension syntax I'm trying to create a left-join using LINQ on two lists that I have. The following is from the Microsoft help but I've modified it to show that the pets list has no elements...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.