The examples thus far have shown matches that are case sensitive. That is, it matters if there is a capital or lower case letter. To turn off case sensitivity, add an i after the last forward slash of the expression. The i stands for insensitive.
Now the expression will hold true for test, Test, TEST, TesT, etc..
Normally a match search is ended after the first occurance is found. There may be times when you want to find all occurances of an element within a string. Using g and a loop, this can be done. The g stands for global. Another feature to bring in is the $&. This is a special built-in variable that holds the value of the last triggered match.
The while block executes as long as there is a match in $string1. The print line uses the built-in variable which always contains the string that triggered the most recent match.
Here is the same script using an array instead of a loop.
| $string =~ /test/i; |
Now the expression will hold true for test, Test, TEST, TesT, etc..
Normally a match search is ended after the first occurance is found. There may be times when you want to find all occurances of an element within a string. Using g and a loop, this can be done. The g stands for global. Another feature to bring in is the $&. This is a special built-in variable that holds the value of the last triggered match.
|
$string1="This is an example string that will trigger several matches."; print "String1 contains $string1 \n"; while ($string1 =~ /[a-z]+/gi){ print "A match was made with $& \n"; } |
The while block executes as long as there is a match in $string1. The print line uses the built-in variable which always contains the string that triggered the most recent match.
Here is the same script using an array instead of a loop.
|
$string1="This is an example string that will trigger several matches."; @results = $string1 =~ /[a-z]+/gi; print "@results \n"; |

