Jump to content

Stripping parentheses and the numbers inside


neuroxik

Recommended Posts

Hey everyone,

 

I'm really bad with regex's but I tried and tried until finally I'm asking for help:

 

I need to replace thousands of lines (I'm already looping each line break, all is okay for that) so that it replaces the numbers that are found within parentheses, like this:

 

1994-12-31 10 12 30 32 34 38 (39)

1994-12-28 10 21 22 27 28 43 (17)

 

would give:

 

1994-12-31 10 12 30 32 34 38

1994-12-28 10 21 22 27 28 43

 

I've tried a whole bunch of combinations with preg_replace such as

 

$s  = preg_replace('/(\(00-99\))/','',$s);

 

but nothing seems to work. As you can see I've escaped the parentheses ( & ) with backslashes so that they are not interpreted as delimiters, but it still doesn't strip the numbers in parentheses, which to some of you guys must be obvious because I must be missing out on something...

 

Hope anyone can help. I hate to ask help so even if you point me to good reference, any help would be extremely appreciated.

 

Thanks

 

 

preg_replace('/(\(00-99\))/','',$s);

Link to comment
Share on other sites

I had to make a few assumptions from your post, but this seems to be in the right ballpark.  This will of course replace (23) but not (231) nor (1).  Seemed to be what you were going for.  If any number of digits inside the () should get stripped then you'd want d+ rather than d{2}. 

 

$s  = preg_replace('/\(\d{2}\)/','',$s); 

Link to comment
Share on other sites

I had to make a few assumptions from your post, but this seems to be in the right ballpark.  This will of course replace (23) but not (231) nor (1).  Seemed to be what you were going for.  If any number of digits inside the () should get stripped then you'd want d+ rather than d{2}. 

 

$s  = preg_replace('/\(\d{2}\)/','',$s); 

 

Wow, it worked! Thanks so much!

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.