PROGRAMMING

  minte9
learningjourney




S R Q

Replace

p50 The text matched (if any) is replaced by replacement.
 
#!/bin/perl -l
=begin

The text matched (if any) is replaced by replacement
For word boundary use \b
For global replacement use \g modifier

=cut

$a = 'Jeff Friedl';
$b = 'Jeffrey Friedl';
$c = 'Jeff is Jeff Friedl';

$a =~ s/Jeff/Jeffrey/;
$b =~ s/\bJeff\b/Jeffrey/;
$c =~ s/\bJeff\b/Jeffrey/g;

print $a; # Jeffrey Friedl
print $b; # Jeffrey Friedl (not Jeffreyrey Friedl)
print $c; # Jeffrey is Jeffrey Friedl

Capture

We want to remove digits from stock proces.
 
#!/bin/perl -l
=begin

Remove digits from stock proces, so
    12.37500000392 is reduce to 12.375, yet
    37.500 is reduced to 37.50
Shorthand \d matches a digit, 
Removes a third posible zero (if any) with [1-9]? 
Wrap in parantheses to capture to $1

=cut

$a = '12.37500000392';
$b = '37.500';

$a =~ s/(\d+.\d\d[1-9]?)\d*/$1/;
$b =~ s/(\d+.\d\d[1-9]?)\d*/$1/;

print $a; # 12.375
print $b; # 37.50

Questions    
Last update: 61 days ago
Perl, Lookaround