ninedoors Posted January 13, 2009 Share Posted January 13, 2009 I have this string : Nick Leigh(1) And I would like to get it so I have an array so that Array ( [0] => Nick [1] => Leigh [2] => 1 ) I am very new to regular expressions but I have been looking at the preg_match function. Is that the one I should be using here to strings that are of this format? Thanks Nick Quote Link to comment Share on other sites More sharing options...
premiso Posted January 13, 2009 Share Posted January 13, 2009 The (1) is the tricky part. Not sure of regex to do this but you can use explode to split a string at the spaces. If there was a space before the ( it would work just fine. Not sure if that is the answer you were looking for but it is sort of an answer. Quote Link to comment Share on other sites More sharing options...
ninedoors Posted January 13, 2009 Author Share Posted January 13, 2009 Yes I knew I could break it apart with explode and then I could use str_replace to get the 1 out of the () but I was hoping that regex could do it in one step rather then 3. Thanks for the input though. Nick Quote Link to comment Share on other sites More sharing options...
.josh Posted January 13, 2009 Share Posted January 13, 2009 $string = "Nick Leigh(1)"; preg_match("~(\w+?)\b\s(\w+?)\(([^\)]+)\)~",$string,$matches); print_r($matches); Quote Link to comment Share on other sites More sharing options...
effigy Posted January 13, 2009 Share Posted January 13, 2009 <pre> <?php $str = 'Nick Leigh(1)'; print_r(preg_split('/[()\s]+/', $str, -1, PREG_SPLIT_NO_EMPTY)); ?> </pre> Quote Link to comment Share on other sites More sharing options...
ninedoors Posted January 13, 2009 Author Share Posted January 13, 2009 Thank you very much Crayon and effigy for your answers. Is there a way I can trouble you for an explanation? I want to learn how to use regex so I can eventually do it myself. Nick Quote Link to comment Share on other sites More sharing options...
effigy Posted January 13, 2009 Share Posted January 13, 2009 preg_split splits a string by expression, discarding the parts that it matches. This expression has two delimiters (//), a character class ([]), a quantifier (+), a shorthand (\s), and two literals (()). The delimiters contain the expression; the character class matches any one character inside, but the plus quantifier instructs it to match 1 or more; and what we're asking it to match is either "(", ")", or any kind of whitespace (\s). Quote Link to comment Share on other sites More sharing options...
.josh Posted January 13, 2009 Share Posted January 13, 2009 Ah damn effigy, forgot about preg_split. Touché. I'd go for effigy's, as it is more elegant. In retrospect, there's a couple of things in my regex has a couple of unnecessary things in it. First one technically works, but I would actually have changed it to: preg_match("~(\w+)\s(\w+)\(([^\)]+)\)~",$string,$matches); (\w+) matches the first word 'Nick'. \w matches an alphanumeric character (lower and uppercase letters, numbers and underscores). + is a quantifier. It tells it to match one or more of those characters. It will stop at the space because space is not an alphanumeric character. The parenthesis means to capture the match and put it into $matches. \s is shorthand for space character (and tabs and spans). Used to look for a space between first name and last name. We don't want to actually capture it; just match it, so the next capture knows where to start. Could have instead physically put a spacebar tap in there, but I think using \s is more visible. Just remember that in general, \s matches not only space, but tabs and other 'whitespace' things. (\w+) matches the second word 'Leigh'. Same as the first one. It starts after the \s and stops when it hits the ( because ( is not an alphanumeric character. \(([^\)]+)\) The text to match is (1). Since parenthesis are special characters for regex, we have to escape it in order for the engine to know to look for a parenthesis instead of treat it as the special character. so \( matches the first ( in (1). The next ( is the opening capture parenthesis for what we want to capture inside the (..); the '1'. [^\)] means to match anything that is not a closing parenthesis ')' (remember, have to escape it). + matches one or more of that. ) is the closing parenthesis for the capture, and \) is the closing parenthesis to literally match. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.