$re = '/(?<!\d)(?<!_)(?<![a-zA-z])string(?!\d)(?!_)(?![a-zA-Z])/m';
$str = 'module mpi_hello_world
use mpi
implicit none
contains
subroutine hello_world(size, & ! testtest
& r_ank, &!
& myhostname &
& )
! this is a comment
! this also
integer, intent(in) :: size, rank23, rank_er,ranking, myrank, my2r_ank
character(len = *), intent(out) :: myhostname
integer :: stat
myhostname = "(no host name)"
#ifdef __INTEL_COMPILER
call get_environment_variable("COMPUTERNAME", myhostname)
#else
stat = hostnm(myhostname)
#endif
write(*, *) "Hello world FORTRAN rank:", rank, " size:", size, &
& "hostname:", trim(myhostname)
end subroutine hello_world
end module mpi_hello_world
';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php