2013-07-25 30 views
1

我想在字符串"$str=Sri Lanka Under-19s 235/5 * v India Under-19s 503/7 "中匹配以下模式India。它應該返回false,因爲不是IndiaIndia Under-19s是否存在?如果只有India在19歲以下,如何使用正則表達式來實現。請幫忙。如何用preg_match匹配特定的字符串?

只有當india存在時它才應匹配,如果存在india under-19則應該失敗。

我寫了下面的代碼這一點,但它始終是匹配 -

$str="Sri Lanka Under-19s 235/5 * v India Under-19s 503/7"; 
$team="#India\s(?!("Under-19s"))#"; 
preg_match($team,$str,$matches); 

回答

2

這確實你問:

<?php 

$str="Sri Lanka Under-19s 235/5 * v India Under-19s 503/7"; 
$team="/India\s(?!Under-19s)/"; 
preg_match($team,$str,$matches); 

exit; 

?> 
1

我的解決辦法:

$text = "Sri Lanka Under-19s 235/5 * v India Under-19s 503/7"; 

$check = explode(" ", strstr($text, "India")); 
if($check[1] == "Under-19s"){ 
    // If is in text 
}else{ 
    // If not 
} 
1

配套缺乏的正則表達式的字符串是有點難看。這是更清楚一點:

$india_regexp = '/india/i'; 
$under19_regexp = '/under-19s/i'; 
$match = preg_match(india_regexp, $str) && ! preg_match(under19_regexp, $str); 
1

假設印度正則表達式之間,並在-19一個空格來檢查,這將是。

/India\s(?!Under)/ 

把所有在一起的代碼

$string = "Sri Lanka Under-19s 235/5 * v India Under-19s 503/7"; 
$pattern="/India\s(?!Under)/"; 
preg_match($pattern,$string,$match); 
    if(count($match)==0){ 
     //What we need 
    }else{ 
     //Under-19 is present 
    } 
相關問題