Page 1 of 1
Solved :: Compilation failure
Posted: Mon Jan 08, 2007 12:35 am
by Zoram
I am trying to use regex in a function of a class and I am getting this error:
Code: Select all
Warning: preg_match() [function.preg-match]: Compilation failed: POSIX named classes are supported only within a class at offset 0 in
And here is the function:
Code: Select all
function getFirstLetter (){
preg_match("[[]]", $this->message['body'], $matches);
return $matches[0];
} // function
Any ideas what would cause the class to throw this error?
Re: Compilation failure
Posted: Mon Jan 08, 2007 12:48 am
by Christopher
Not sure what you are trying to do, but maybe something like this:
Code: Select all
function getFirstLetter (){
preg_match("/[a-zA-Z]/", $this->message['body'], $matches);
return $matches[0];
} // function
Posted: Mon Jan 08, 2007 12:51 am
by Zoram
That is weird, i apologize, for some reason it won't put the :alpha: in the php function code.. it keeps taking it out. so in the [[]] there should be a :alpha:.
the preg match was working fine before I put it into the class and tried to use it there. I am trying to figure out if there is something that prevents the preg_match() from working in classes... or where that error is coming from.
Posted: Mon Jan 08, 2007 8:52 am
by feyd
The syntax highlighter (or somewhere between) removes them. Not sure why, don't have the energy to figure it out. It's better to avoid the POSIX forms anyways.
The pattern shouldn't have ever worked. As it stands it will consider the leading "[" as the delimiter.
Posted: Mon Jan 08, 2007 9:46 am
by Zoram
I am curious why the /[a-zA-Z]/ and the [[a-zA-Z]] both worked... What does the / and the beginning and end do? what does the [[ and ]] do?
Posted: Mon Jan 08, 2007 9:52 am
by feyd
PCRE uses delimiters to mark where the pattern begins and ends so modifiers can be added that affect the pattern's functionality.
Any symbol is accepted as a delimiter although it is often suggested to avoid ones that are already metacharacters. This includes []{}()|+=-?!$^*.
I personally almost always use #. Some others around here use @ and ~. In Perl / is the only allowed delimiter (that I remember.)
Posted: Mon Jan 08, 2007 10:03 am
by Kieran Huggins
Until seeing examples on this board a few weeks ago I thought only the / delimiter was allowed.
Javascript's regex insists on / delimiter as well, btw.
Posted: Mon Jan 08, 2007 10:05 am
by feyd
For short-hand regex in Javascript, yes.
Posted: Mon Jan 08, 2007 1:19 pm
by Mordred
The reason it worked is that bracket-type delimiters work in opening/closing pairs unlike other characters:
The expression should be enclosed in the delimiters, a forward slash (/), for example. Any character can be used for delimiter as long as it's not alphanumeric or backslash (\). If the delimiter character has to be used in the expression itself, it needs to be escaped by backslash. Since PHP 4.0.4, you can also use Perl-style (), {}, [], and <> matching delimiters.
I personally like the ~tilde~ character, it makes more easily read patterns, quite unlike /slash/ which sometimes makes beautiful ascii art if you escape a lot, but is not very /\"readable\"/ as code
