capture items between [ ]

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

Moderator: General Moderators

Post Reply
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

capture items between [ ]

Post 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);
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I am pretty bad with regex, but try:

Code: Select all

preg_match('/\[(.*)\]/', $subject, $matches);
print_r($matches);
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

Thanks ninja that worked!
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

:: kicks back in chair ::

16 pages into my new regex book... aaah how I love O-Reilly. :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

That pattern is greedy... ;)
kingconnections
Forum Contributor
Posts: 137
Joined: Thu Jul 14, 2005 4:28 pm

Post by kingconnections »

Ya I used the ? to stop it! ;)
Superman859
Forum Commoner
Posts: 47
Joined: Sun Oct 29, 2006 10:22 am

Post 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.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
Post Reply