Solved :: Compilation failure

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Solved :: Compilation failure

Post 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?
Last edited by Zoram on Mon Jan 08, 2007 9:30 am, edited 2 times in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Compilation failure

Post 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
(#10850)
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Zoram
Forum Contributor
Posts: 166
Joined: Sun Aug 18, 2002 3:28 pm
Location: Utah
Contact:

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.)
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

For short-hand regex in Javascript, yes.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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 ;)
Post Reply