Page 1 of 1

Simple Regular Expression

Posted: Thu Mar 11, 2004 5:22 pm
by pickle
Hi All,

Regular expressions are weird and scary to me, but this seems pretty simple.

I'm trying to chunk through an LDAP domain name. An example string would be:

Code: Select all

cn=ART999,ou=testobject,o=University
Now, what I'm trying to do is determine if the above string has a course in it. The course will always be of the form XXXYYY where X = an alphabetic character and Y = an integer. Ideally I'd like only the course name returned, but if it doesn't happen, oh well.

The regular expression I've built so far goes like this:

Code: Select all

$name = split("cn=їa-zA-Z]{3}ї0-9]{3}",$passed_object_name);
Then, I do a test on $name and if it's populated, I found a hit.

For some reason, $name[1] will get everything after the match, but $name[0] is empty. So from what I can figure, split() is finding the match, just not putting it in $name correctly. Any thoughts? Thanks.

Posted: Thu Mar 11, 2004 6:12 pm
by Weirdan
split is intended for splitting =) So pattern describes the 'element delimiter' by which the source string is splitted then. Use [php_man]eregi[/php_man] to capture substring like yours.

Code: Select all

if(eregi("(cn=їa-zA-Z]{3}ї0-9]{3})",$passed_object_name,$matches)) {
   echo $matchesї1]; // prints 'cn=XXXYYY'
 }

Posted: Fri Mar 12, 2004 9:36 am
by pickle
Awesome, thanks. Since you seem to be somewhat versed in regular expressions - could you tell me if there's a simple way to just have XXXYYY returned, or do I have to break apart the string afterwards? Thanks again.