2017-02-26 99 views
1

我想检查是否字符串包含任何形式的以下模式如何找到,如果字符串包含的子字符串格式(spaceNUMBER-NUMBERspace)

空间 NUMBER-NUMBER 空间)的实例(1 -0)

类似:

$str1 = '21-02-2017 - child 1-9 my parent'; 
$str2 = '21-02-2017 - child - my parent'; 

$bool_val= True; 

if(checkContain($str1)) 
{ 
    $bool_val = True; //True because contains (1-9) 
} 

if(checkContain($str2)) 
{ 
    $bool_val = False; 
} 

function checkContain($str){ 
    // ?? 
} 

谢谢!

+0

您可能想检查在[官方PHP网站](http://php.net/manual/en/pcre.pattern.php)中找到的文档。当然,所有[PCRE函数](http://php.net/manual/fr/ref.pcre.php)都可以使用PCRE规范。 –

回答

1

可以做一个简单的正则表达式:

function checkContain($str){ 
    return preg_match("/ [0-9]-[0-9] /",$str); 
} 

添加旁边[0-9]一个+匹配任何数量的数字

+0

太好了,非常感谢 –

1

可以实现,使用正则表达式如:

\s\d+|\d+\s 

而且你必须使用php中的方法来匹配这个模式,如果它返回的话将返回true e是任何其他匹配false

相关问题