If the pattern delimiter character appears in the pattern it must be escaped so that the regexp processor will correctly interpret it as a pattern character and not as the end of the pattern. This would produce a regexp error: /ldap://*/ but this is OK: /ldap:\/\/*/ Therefore if you choose another delimiter altogether you don't have to escape the slashes: #ldap://*# Cleaner and more clear.
Ok, that makes sense.
>preg_match('|^ldap(s)?://[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$|', $this->server
)
>> >>I also recommend using single quotes instead of double quotes here. > >Single Quotes: Noted. Any reason why? I guess you might be a little out
of
>luck putting $vars into a regex without . concatenating. Both PHP and regexp use the backslash as an escape. Inside double quotes, PHP interprets \ as escape, while inside single quotes PHP interprets \ as a simple backslash character. When working with regexp in PHP you're dealing with two interpreters, first PHP and then regexp. To support PHP's interpretation with double quotes, you have to escape the escapes: Single quotes: '/ldap:\/\/*/' Double quotes: "/ldap:\\/\\/*/" PHP interprets "\\/" as \/ RegExp interprets \/ as /
Oh. Duh! I wasn't even considering PHP parsing the string due to the double quoted string.
So, for a pattern like this that contains slashes, it's best to use a non-slash delimiter AND single quotes (unless, as you say, you need to include PHP variables in the pattern): $pattern = '#ldap://*#'; Personally I favor heredoc syntax for such situations because I don't have to worry about the quotes: $regexp = <<<_ #ldap://*$var# _;
Yeah, I just wish there were some way heredoc could work on one line.
>>why is there a period in the second pattern? > >The period comes from the original article on SitePoint (linked earlier).
Is
>it unnecessary? I can't say I'm real sure what this means for the '.' in >regex's: > >"Matches any single character except line break characters \r and \n.
Most
>regex flavors have an option to make the dot match line break characters >too." >- http://www.regular-expressions.info/reference.html Inside of a bracketed character class, the dot means a literal period character and not a wildcard. "All non-alphanumeric characters other than \, -, ^ (at the start) and the terminating ] are non-special in character classes"
So what does the definition I posted mean for non-bracketed periods? Does it mean it will match anything but a line or return break character? How in practice is this useful?
PHP PREG Pattern Syntax http://www.php.net/manual/en/reference.pcre.pattern.syntax.php scroll down to 'Square brackets' >>Also, why are you allowing for uppercase letters >>when the RFC's don't allow them? > >I hadn't gotten far enough to strtolower(), but that's a good point, I >hadn't actually considered it yet. Perhaps it has to do with the source of the string: can you guarantee that the URIs passed to this routine conform to spec?
I just prefer to use strtolower(). I have to use the server address anyways... Breaking News: I had a thought (surprise!). Are LDAP servers ever on localhost? Or at least a non-dot-concatenated address (ldap://directoryname)? The pattern we've been looking won't match that, I think.
Another way to handle this would be to simply accept case-insensitive
strings:
|^ldap(s)?://[a-z0-9-]+\.[a-z.]{2,5}$|i
I actually read about that a little while ago, I just didn't know where to put the i. Thanks!
Pattern Modifiers http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php "i (PCRE_CASELESS) " If this modifier is set, letters in the pattern match both upper and lower case letters."
How do you test regex's against any known variants? I suppose I need to build a test function to make arbitrary strings and then test and print the results. I just don't know if my regex is going to be that great in practice. This would be in addition to the program Richard alluded to in the code checker. Thanks! -- Jared Farrish Intermediate Web Developer Denton, Tx Abraham Maslow: "If the only tool you have is a hammer, you tend to see every problem as a nail." $$