tags:

views:

91

answers:

5

I want to match everything in a string that does not match a given pattern; for example [a-z].

Given the string abc4jkf8 4à3in, I need to match 48 4à3.

I've tried with ([a-z])+(?![a-z]) but this matches exactly the opposite of what I need. With the string above, this regexp matches abcjkfin.

Any ideas?

+6  A: 

You use a negative set:

([^a-z]+)
Guffa
OMG, i tryed that one too, but i did wrote it as `(^[a-z])`..
DaNieL
So, you didn't tryed that one.
salathe
+1  A: 

You need to match any charater that is no alpha. The ^ tells not to match alpha chars

[^a-z]*
skyfoot
Does a-z contain umlauts or letters with accents like ä, ü, á, é?
Simon
@Simon: no, AFAIK `òèàìù` are not included into the [a-z] set
DaNieL
@Simon: @DaNiel is right, a-z does not include accents. If you wanted to match them you may need to add them yourself like [àä]
skyfoot
@skyfoot, @Simon: or use `\p{IsLetter}`, which includes *all* Unicode accented characters you could ever imagine (negative character class: `\P{IsLetter}` to match anything *not* a letter). You can use the character shortcuts inside and outside a character class.
Abel
+1  A: 
$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');
}

gives what you want.

Artefacto
That's similar to what im doing now, but i want to do it with just 1 preg_replace
DaNieL
+2  A: 
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] matches every character that is not a-z.

Spidfire
+2  A: 

why not use preg_replace.

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

this gives the desired result

Anthony