dsaba Posted January 14, 2008 Share Posted January 14, 2008 Here is the sample haystack: hi my a name b is I want to match the string "hi my name is" without the 'b' and the 'a' I know how to skip over words, or not match them when they come on the ends of strings, but not when they come in the middle of the string. ie these two examples skip over the 'a' ~hi my name is (?=a)~ in "hi my name is a" ~(?<=a) hi my name is~ in "a hi my name is" I'd like to see how this can be done, or if it can be done at all with the PCRE engine in PHP. Thanks Quote Link to comment Share on other sites More sharing options...
effigy Posted January 14, 2008 Share Posted January 14, 2008 <pre> <?php preg_match('/\A((??!\ba\b).)+)a((??!\bb\b).)+)b(.*)\z/', 'hi my a name b is', $matches); print_r($matches); array_shift($matches); foreach ($matches as &$match) { $match = trim($match); } echo implode(' ', $matches); ?> </pre> Quote Link to comment Share on other sites More sharing options...
dsaba Posted January 15, 2008 Author Share Posted January 15, 2008 In that case I could have just used ~(hi my )a( name )b( is)~ instead of your pattern, because it yields the same results. I wanted to see if this string alone could be the full match "hi my name is" In your regex pattern, the full match is "hi a my name b is", so you didn't "skip" over any words, and the reason for the output is because of php code, not regex. The two example matches I provided were quite easy, but in my question of only matching "hi my name is" in that haystack...this boggled my mind. I didn't know if this could be done, so I take it you can't do it then? Quote Link to comment Share on other sites More sharing options...
manixrock Posted January 15, 2008 Share Posted January 15, 2008 As far as I'm aware a regexp can only catch a part of the string it matches, and substrings of that in groups. So I don't think it's possible to 'skip' the string in the main match, if that was your intent. Quote Link to comment Share on other sites More sharing options...
effigy Posted January 15, 2008 Share Posted January 15, 2008 In that case I could have just used ~(hi my )a( name )b( is)~ instead of your pattern, because it yields the same results. Your pattern assumes that it knows the content, which defeats the purpose of using a pattern at all, whereas mine only assumes that you know the words you want to skip. In your regex pattern, the full match is "hi a my name b is", so you didn't "skip" over any words The keyword here is "full." According to the documentation "$matches[0] will contain the text that matched the full pattern," and this is inescapable, as manixrock touched on. 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.