tags:

views:

1319

answers:

4

I'm trying to strip all punctuation out of a string using a simple regular expression and the php preg_replace function, although I get the following error:

Compilation failed: POSIX named classes are supported only within a class at offset 0

I guess this means I can't use POSIX named classes outside of a class at offset 0. My question is, what does it means when it says "within a class at offset 0 "?

$string = "I like: perl";    

if (eregi('[[:punct:]]', $string))  
    $new = preg_replace('[[:punct:]]', ' ', $string); echo $new;
+6  A: 

The preg_* functions expect Perl compatible regular expressions with delimiters. So try this:

preg_replace('/[[:punct:]]/', ' ', $string)
Gumbo
Fantastic, not only does this solve my problem, but I also learnt something new about how preg_* works. Thanks :)
+3  A: 

NOTE: The g modifier is not needed with PHP's PCRE implementation!

In addition to Gumbo's answer, use the g modifier to replace all occurances of punctuation:

preg_replace('/[[:punct:]]/g', ' ', $string)
//                         ^

From Johnathan Lonowski (see comments):

> [The g modifier] means "Global" -- i.e., find all existing matches. Without it, regex functions will stop searching after the first match.
strager
I'm pretty new to the complexities of regular expressions, what "specifically" is the definition of 'g'? I'd assume from you've said that it's a way of telling the regexp to be recursive?
@chris: It means "Global" -- i.e., find all existing matches. Without it, regex functions will stop searching after the first match.
Jonathan Lonowski
@Lonowski, thanks; I have updated my answer with your information.
strager
/g means global, which tells regex to replace all instances.echo "What's up?"|sed "s/[[:punct:]]/ /"What s up?echo "What's up?"|sed "s/[[:punct:]]/ /g"What s upHowever, strager's wrong. preg_replace is /default/ global.echo preg_replace('/[[:punct:]]/', ' ', "What's up?");What s up
Matthew Flaschen
In this aspect the PHP implementation is not really following the Perl standard. The preg_replace() function always replaces all matches.
Gumbo
Wow, that comment looks bad with all the spacing screwed up. Oh well.
Matthew Flaschen
@Flaschen, Gumbo: I did not know that! I guess there's a few bugs in some regular expressions I've written in the past, then!
strager
There’s a list of modifiers that are supported: http://docs.php.net/manual/en/reference.pcre.pattern.modifiers.php
Gumbo
A: 

An explanation of why you're getting that error: PCRE uses Perl's loose definition of what a delimiter is. Your outer []s look like valid delimiters to it, causing it to read [:punct:] as the regex part.

(Oh, and avoid the ereg functions if you can - they're not going to be included in PHP 5.3.)

Ant P.
Is there an alternative to ereg_replace for what I'm doing?
@chris, He said ereg. You're using preg_replace. You're also using eregi (which isn't necessary, really, in this situation) which is what @Ant was talking about.
strager
Sorry, moment of zero concentrtion. I see what he's saying now.
A: 

I just added g to the regexp as suggested in one of the anwers, it did the opposite of wahts expected and DIDN'T filter out the punctuation, turns out preg_replace doesnt require g as it's global/recursive in the first place