开发者

perl :Extract first line from content

Requirement : I am having content with many lines. I need to extract first l开发者_开发技巧ine and add new line after that.

Condition : First line can be ended with . , ? , ! and followed by space with Capital letter or any number . There might be already new line after . , ? , !. In that case we need to replace those extra new line with single line

For example if content is

Case1

My name is abc. I am working in Software..... 

or

Case2

My name is abc.
I am working in Software...

In both the cases result should come like

My name is abc.
I am working in Software...

Solution : what i have tried :

   $$text =~ s/(.+?[\.\?!$])(\n*)(\s[A-Z0-9])/$1\n$3/smi ;

It is working fine with 2nd case. But it is not adding new line in first case. Please suggest


Why do you put $ in your character class ? And why use $$text ?

You could try :

#!/usr/bin/perl
use 5.10.1;
use strict;
use warnings;

my @l = (
"My name is abc. I am working in Software..... ",
"My name is abc.
I am working in Software... 
");

for(@l) {
  s/([.?!])(\n*)\s*/$1\n/smi ;
  say;
}

Output:

My name is abc.
I am working in Software..... 
My name is abc.
I am working in Software... 


#!/usr/bin/perl
use strict; use warnings;

my @strings = (
    "My name is abc.\nI am working in Software...",
    "Is your name xyz?\n \n How do you do?",
    "My car is red!\n Fire engine red!",
    "Mr.\nBrown goes to Washington.",
);

for my $s ( @strings ) {
    $s =~ s/^( [^.?!]+ [.?!]) \s+ /$1 /x;
    print $s, "\n";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜