正则表达式我将如何搜索星号 - 空格 - 等号?

时间:2011-06-16 08:23:31

标签: php regex

用什么正则表达式来搜索星号,然后是空格后跟一个等号?

'* ='

在php中使用preg_match会在这些字符串中找到匹配项:

'yet again * = it happens'

'again* =it happens'

什么是最简单的方法来搜索一个确切的单词,数字的数字,在正则表达式的惩罚标志字符串的惩罚标志?

3 个答案:

答案 0 :(得分:2)

这里不需要正则表达式。考虑使用strpos

$pos = strpos('yet again * = it happens', '* =');
if(pos === false){
    // not there
}
else {
    // found
}

如果您必须使用preg_match,请记住分隔符:

preg_match('/\* =/', $str, $matches);

在这种情况下,你必须逃避星号。您可能希望允许更多空格,例如使用模式\*\h+=\h代表horizontal whitespace characters

答案 1 :(得分:0)

试试这个正则表达式

\*(?=\s=)

答案 2 :(得分:0)

<?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!