regex how would i search for asterisk-space-equals sign?
what regex would be used to search for an asterisk followed by a space followed by an equlas sign?
that is '* ='
?
Using preg_match in php would find a match in these strings:
'yet again * = it happens'
'again* =it happens'
and what is simplest way to search for an exact word for word, number for number, puncuation sign for 开发者_JS百科puncuation sign string in regex?
You don't need a regular expression here. Consider using strpos
$pos = strpos('yet again * = it happens', '* =');
if(pos === false){
// not there
}
else {
// found
}
If you must use preg_match
, remember the delimiters:
preg_match('/\* =/', $str, $matches);
In this case you have to escape the asterisk. You may want to allow more spaces, for example with the pattern \*\h+=
. \h
stands for horizontal whitespace characters.
Try this regex pattern
\*(?=\s=)
<?php
function runme($txt) {
return preg_match("/^\*\ \=$/", $txt);
}
echo runme($argv[1])? "$argv[1] matches!\n" : "$argv[1] is not asterisk space equals-sign\n";
?>
$ php asterisk_space_equals.php 'yet again * = it happens'
yet again * = it happens is not asterisk space equals-sign
$ php asterisk_space_equals.php 'again* =it happens'
again* =it happens is not asterisk space equals-sign
$ php asterisk_space_equals.php '* ='
* = matches!
精彩评论