Page 1 of 1

capture items between [ ]

Posted: Fri Feb 09, 2007 2:35 pm
by kingconnections
I am have the need to extract a string that in enclosed in [].

The data looks like so:

Code: Select all

CN=Wonka\,Willy [WWonka],OU=test,OU=EastCoast,OU=People,DC=corp,DC=Wonka,DC=com
I need to extract the [WWonka] which in this case is the user name.

Here is what I have so far: I have been using ereg, just cause I don't know any better yet!

Code: Select all

if (ereg('\[*\]',$info[$x]["member"][$i])){
  echo "". $info[$x]["member"][$i]."<br>";
  ereg('{[}\w*{]}]',$info[$x]["member"][$i],$test);
  echo "<BR>";
  print_r($test);

}
Below returns the users I want to see, but the second ereg does not contain any matches in $test.

I tried the folllowing as well but no dice!

Code: Select all

ereg('\[*\]',$info[$x]["member"][$i],$test);

Posted: Fri Feb 09, 2007 2:41 pm
by Luke
I am pretty bad with regex, but try:

Code: Select all

preg_match('/\[(.*)\]/', $subject, $matches);
print_r($matches);

Posted: Fri Feb 09, 2007 4:41 pm
by kingconnections
Thanks ninja that worked!

Posted: Fri Feb 09, 2007 4:55 pm
by Luke
:: kicks back in chair ::

16 pages into my new regex book... aaah how I love O-Reilly. :D

Posted: Fri Feb 09, 2007 7:00 pm
by feyd
That pattern is greedy... ;)

Posted: Sat Feb 10, 2007 1:03 am
by kingconnections
Ya I used the ? to stop it! ;)

Posted: Mon Feb 12, 2007 11:50 am
by Superman859
That O'Reilly book is a good choice! I'm About 200 pages into it myself :wink: I haven't even started using the regex info that I know (that comes in later chapters).

As for the expression, (from what I understand) the .* combo should match the username in all cases as long long as it starts and ends with that [ and ]. Even if it is greedy, it will still work. The regex will read in the rest of the line, and then backtrack one step at a time until it gets back to the ] after username. That simply means it's going to waste a lot of time going through and matching through to DC=com, and then backtrack one step at a time.

An alternative to making it lazy would simply be to use the expression

[^\]]*

It looks a bit strange at first, but this simply means match anything other than a closing square bracket, any number of times. It will simply read in the full user name and stop when it hits the closing square bracket, which is exactly what you want it to do. The backslash was used to cancel out the bracket so it is taken literally.

Posted: Mon Feb 12, 2007 11:57 am
by Luke
nice... thank you for that... like I said I'm a regex n00b. I never realize just how cool they are until this book.