Regexp: desen içine değil everythings maç nasıl?

5 Cevap php

I not belirli bir desenle eşleşen yapan bir dize her şeyi maç istiyorum; örneğin, [a-z].

Dize verilen abc4jkf8 4à3in, eşleştirmek gerekir 48 4à3.

I ([a-z])+(?![a-z]) ile denedim ama bu ne gerek tam tersini eşleşir. Yukarıdaki dize ile, bu regexp maçlar abcjkfin.

Herhangi bir fikir?

5 Cevap

Eğer bir negatif seti kullanın:

([^a-z]+)

neden preg_replace kullanmayın.

$string = "abc4jkf8 4à3in";
echo preg_replace("/[a-z]/", "", $string);

Bu istenen sonuç verir

preg_match_all('/([^a-z]+)/si', $code, $result, PREG_PATTERN_ORDER);
$unmached = "";
for ($i = 0; $i < count($result[0]); $i++) {
    $unmached .= $result[0][$i];
}
echo $unmached;

[^a-z] a-z değil, her karakter ile eşleşir.

Hiçbir alfa herhangi charater maç gerekir. ^ Alfa karakter maç için değil söyler

[^a-z]*
$a = "abc4jkf8 4à3in";

function stringConcat($a, $b) { return $a.$b; }

if (preg_match_all("/[^a-z]/", $a, $matches)) {
    echo array_reduce(reset($matches), 'stringConcat');
}

Ne istediğinizi verir.