Monday, 21 October 2019

how many ways to get last word of a string using regular expression in perl?

Method 1:

$tex="nice perl you are";
$tex=~/(\S+)$/;
print "$1\n";

Method 2:

$tex="nice perl you are";
$tex=~/(.*)\s+(\w+)$/g;
print "$2\n";

Method 3:

$tex="nice perl you are";
$tex=~/([A-Za-z]+)$/;
print "$1\n";

Method 4:

$tex="nice perl you are";
$tex=~/(\b[A-Za-z]+\b)$/;
print "$1\n";

Method 5:

$tex="nice perl you are";
$tex=~/([\w|.]+)$/g;
print "$1\n";

Method 6:

$tex="nice perl you are";
$tex=~/([\S|.]+)$/g;
print "$1\n";

Method 7:

$tex="nice perl you are";
$tex=~/([\S|.]+)\Z/g;
print "$1\n";

Method 8:

$tex="nice perl you are";
$tex=~/(.*)\s+(.*)$/g;
print "$2\n";

Method 9:

$tex="nice perl you are";
$tex=~/(\w+)\s+(\w+)$/g;
print "$2\n";

Method 10:

$tex="nice perl you are";
$tex=~/\S+\s+(\S+)$/g;
print "$1\n";

Method 11:

$tex="nice perl you are";
$tex=~/([^\s]*)$/g;
print "$1\n";

Method 12:

$tex="nice perl you are";
$tex=~/([\w\d\S._-]*)$/g;
print "$1\n";

Method 13:

$tex="nice perl you are";
$tex=~/(?:$|(?:\.\s))*(\w+)$/g;
print "$1\n";

Method 14:

$tex="nice perl you are";
$tex=~/(?m)\W*(\w+).*?(\w+)\W*$/;
print "$2\n";

Method 15:

$tex="nice perl you are";
$tex=~/\s*(\w+)[^;]+\s(\w+);?.*/g;
print "$2\n";



0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home