The preg_grep
function in PHP is a useful function to find
entries in an array that match a given pattern. Ruby does this same operation
with the grep
method.
In this example, we'll build a new array that only consists of email addresses
that end in .com
.
{{code:php
$myArray = array('[email protected]', '[email protected]');
var_export($result);
// => array('[email protected]')
}}
{{code:ruby my_array = ['[email protected]', '[email protected]'] result = my_array.grep(/.com$/)
p result
# => ["[email protected]"]
}}