Skip to main content
GameDev.net gamedev.net
🔒 Locked

Regular expressions question...

Started by Senses777 Jul 15, 2004 at 12:52 AM 4 replies 400+ views
Original Post
Senses777
Senses777
I can't figure out a way to google this, and every regular expressions tutorial I'v found skips this then suddenly starts using it, leavin me dumbfounded. What is the meaning of forward slashes in (PHP) regular expressions? For example, a regular expression match to see if there are any non alpha numeric characters: "/[^a-z,A-Z,0-9_]/". What do the forward slashes do?
"I want to make a simple MMORPG first" - Fenryl
SiCrane
SiCrane
PHP regular expressions need to be deliminated. / is commonly used for this, but anything except an alphanumeric or a \ can be used. You might also see perl style (), {}, [], and <> matching deliminators, but they don't seem to be common in PHP. (And IMO, the [] one is just evil.) Basically everything between the /'s are the regular expression, and various types of modifiers can follow the regex after the deliminator. If the deliminator appears in the regex it needs to be escaped with \.
bastard2k5
bastard2k5
I think it is simply treat the expression between the two forward slashes, as a single regex statement, although my regex experience is limited, and with python, so this is only a slightly educated guess.
Senses777
Senses777
Quote:
Original post by SiCrane
PHP regular expressions need to be deliminated. / is commonly used for this, but anything except an alphanumeric or a \ can be used. You might also see perl style (), {}, [], and <> matching deliminators, but they don't seem to be common in PHP. (And IMO, the [] one is just evil.) Basically everything between the /'s are the regular expression, and various types of modifiers can follow the regex after the deliminator. If the deliminator appears in the regex it needs to be escaped with \.


I understand it more now, but could you give me some examples? Should I always use these delimiters? What exactly can follow them? Can there be more than one set of delimeters? ("/expression/ something /another expression/"?
"I want to make a simple MMORPG first" - Fenryl
SiCrane
SiCrane
Deliminaters should always be used, I'm pretty sure recent versions of PHP will reject regex's without valid deliminators.

Off the top of my head valid modifiers include: i, which makes the regex case insensitive; s, which causes . to match new lines; and S, which optimizes certain classes of regexes if you plan to use them more than once. A PHP reference should have a more complete list.

I'm pretty sure if you try to create a regex with more than one set of deliminators, PHP will reject it, since the second regex will be interpreted as an invalid modifer.
Senses777
Senses777
Thanks! I always knew they were usefull but they looked somewhat intimidating. I needed to learn them for the project I am working on right now.
"I want to make a simple MMORPG first" - Fenryl

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.