lordbry Posted August 9, 2007 Share Posted August 9, 2007 Hey there, I have a love/hate relationship with regular expressions. I love how powerful they are and hate how mindnumbingly complex they can be to put together. Any help would be greatly appreciated here. I'm trying to do this one thing. If the input string is: Abc (123 abc stuff) 1xyz I want to strip out the ()'s and everything between them to end up with: Abc 1xyz Additionally, if there's a way that any of you know how to take this: ABC ID #: 123456 $123.45 ... and turn it into this... ABC $123.45 ... I would also appreciate that. Many many thanks in advance for any help with these issues. - Bry Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted August 9, 2007 Share Posted August 9, 2007 <?php $pattern = '/\(.*\)/'; $replace = ''; $string = 'Abc (123 abc stuff) 1xyz'; echo preg_replace($pattern, $replace, $string) ?> Edit, here's the other one <?php $pattern = '/ID #: [0-9].* /'; $replace = ''; $string = 'ABC ID #: 123456 $123.45'; echo preg_replace($pattern, $replace, $string) ?> Best, Patrick P.S. I think what the previous poster was trying to say is that there is a regex board here where you might get a quicker response. Quote Link to comment Share on other sites More sharing options...
lordbry Posted August 9, 2007 Author Share Posted August 9, 2007 You sir are my hero! Many thanks! And oddly enough, your solutions actually make sense to me whereas most RegEx patterns I see simply look like gibberish. Sorry about posting in the wrong place. I'll try not to do that again in the future. Thanks! - Bry Quote Link to comment Share on other sites More sharing options...
effigy Posted August 9, 2007 Share Posted August 9, 2007 For the first, make your quantifier ungreedy (.*?) if the string may contain more than one set of parentheses. For the second, will the ID always be numeric? If so, use \d+. Otherwise, you're matching a starting digit that could be followed by anything. 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.