不要preg_match_all這裏
$regex = '/(?J)I wanna match pattern (?P<number>1) which is very different from pattern 2|(?P<number>2), again nothing to do with pattern 1 here/';
結果使用帶有preg_match
:
array(3) {
[0]=>
string(62) "I wanna match pattern 1 which is very different from pattern 2"
["number"]=>
string(1) "1"
[1]=>
string(1) "1"
}
完全固定的正則表達式'nothing similar' was not found in the orignal regex
:
$text1 = 'I wanna match pattern 1 which is very different from pattern 2';
$text2 = 'I wanna match pattern 2, again nothing similar with pattern 1 here';
$regex = '/(?J)(I wanna match pattern (?P<number>1) which is very different from pattern 2|I wanna match pattern (?P<number>2), again nothing similar with pattern 1 here)/';
echo "Pattern 1\n";
preg_match($regex, $text1, $matches);
var_dump($matches);
echo "\n\nPattern 2\n";
preg_match($regex, $text2, $matches);
var_dump($matches);
請您通過提供一些簡單的輸入澄清你的問題和他們的預期產出?據我所知,'preg_match_all('/ \ d + /',$ input,$ out)'或'preg_split('/ [^ \ d] + /',$ input,null,PREG_SPLIT_NO_EMPTY)'會做工作。讓我們繼續解決您的問題,以便將來的SO讀者可以從您的問題中受益。 – mickmackusa