|
|
=~ String searching in Perl - Perl
|
Views : 105
|
|
Tagged in : Perl
|
|
|
Report This Scrap as Inappropriate We request you to choose the appropriate categroy and subcategory that suits your
objectionable concern about the scrap, So that our team can review and find out whether it violates our Guidelines or the
scrap is not suitable for all viewers.
|
=~ String searching in Perl
The '=~" syntax is used to find/match specific contents in a variable.
$x = "One two three"
if ($x =~ /two/) (is true)
Use the '^' to signify 'starting with'
if ($x =~ /^two/) (is false, starts with one)
Use a trailing 'i' command to ignore case
if ($x =~ /one/i) (Is true because case is ignored)
Use '\b' to check for word boundaries
if ($x =~ /thre/) (Is true because 'thre' of 'three' is found)
if ($x =~ /thre\b/) (Is false because there is no word 'thre')
Use '\W' to find the first non-word character.
Use '.*' to represent current find results to end of value
Use a starting 's' to represent substitute and a trailing 'newvalue/'
By combining these commands you can extract the first word in a variable.
$saveoriginal = $x;
$x =~ s/\W.*//;
value of $x is now 'One'.
|
|
By Geethalakshmi, On - 2010-09-17 |
|
|
|