Jump to content

Help with regular expressions?


lordbry

Recommended Posts

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

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.