develooper Front page | perl.beginners | Postings from April 2010

RE: Extract substring from offset to space or full stop

Thread Previous | Thread Next
From:
Mimi Cafe
Date:
April 18, 2010 08:14
Subject:
RE: Extract substring from offset to space or full stop
Message ID:
4bcb21af.8e83e30a.761a.2a12@mx.google.com
Hi,

It works fine and I like it. My regex is not that good, but I can see what
is doing. I modified it a bit (to capture up till a full stop sign).

<Code>
#!/usr/bin/perl
#
use strict;
use warnings;
#
my $str = "The black cat is trying to climbed the green tree. This time it
failed."; print "string: $str\n";
my $sbstr = substr $str, 0,17;
print "The substr function with length 17 will capture: $sbstr\n";
$str =~ m{ \A ( .{17} .*?\. ) \s }msx;
my $extracted = $1;
print "The pattern with quantifier set 17, extracted: $extracted\n"; <Code>
<Code>


Outputs:

string: The black cat is trying to climbed the green tree. This time it
failed.
The substr function with length 17 will capture: The black cat is
The pattern with quantifier set 17, extracted: The black cat is trying to
climbed the green tree.


Cool





-----Original Message-----
From: Akhthar Parvez K [mailto:akhthar@sysadminguide.com] 
Sent: 18 April 2010 15:45
To: beginners@perl.org
Subject: Re: Extract substring from offset to space or full stop

Hi Shawn,

> $str =~ m{ \A ( .{15} .*? ) \s }msx;

I don't think this would work if the value given in the match string (15 as
per above eg.) is greater than the character count of the particular string.
Right?

Regards,
Akhthar Parvez K
http://Tips.SysAdminGUIDE.COM
UNIX is basically a simple operating system, but you have to be a genius to
understand the simplicity - Dennie Richie

On Sunday 18 Apr 2010, Shawn H Corey wrote:
> #!/usr/bin/perl
> 
> use strict;
> use warnings;
> 
> my $str = "The black cat climbed the green tree";
> print "string: $str\n";
> 
> my $extracted = $1;
> print "extracted: $extracted\n";



-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/




Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About