PHP regex expression to capture and replace part of string

Say I have a string and within that string are multiple occurrences of a string similar to "@41|8:00|10/01/2015@".
What I need to do is find each of these strings and remove everything between the two "|" so "@41|8:00|10/01/2015@" needs to become "@41|10/01/2015@".
I   can find and replace the entire string with
preg_replace('/@[^>]*@/', 'replaced', $teststr);

Open in new window

but how do I find a replace only that center portion?
I just cannot get my head around the expression...
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Something like this
$string = "@41|8:00|10/01/2015@a afadf af afasdf asf af @42|8:00|11/01/2015@ a afasdfaf af a a  @43|8:00|12/01/2015@";

$result = preg_replace('/(@\d+)\|.*?\|(.*?@)/', '\1|\2', $string);
echo $result;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
steamngnAuthor Commented:
Right on the money! Ok, without taking up your entire day can you break this out a bit and explain it to me? I fought with this all day and you hit it out of the park first pitch<insanely jealous></insanely jealous>
steamngnAuthor Commented:
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Julian HansenCommented:
Expression Explained

(@\d+) - match anything that starts with a '@' followed by one or more digits (\d). The ( ) around the expression means we want to save it as a parameter so that we can use it in the replace

\| - | is a special char in regex (means or) - so we have to escape it with a '\' - we don't need it because we will recreate it in the replace.

.*? - no ( ) because we are not interested in this bit. the .* says match anything the ? says don't be greedy.
.* will match anything which would include the next \| - however we want it to stop matching when it hits the \| so we put a ? after the .* to say don't be greedy take only what you can up until the next thing we are looking for - in this case the \|
\| - the next | delimiter
(.*?@) - the ( ) says keep this for use in the replace (this will be parameter #2) - says match anything but don't be greedy (?) until you hit a @ - include that in the match and then stop.

Why do we not put a ? (don't be greedy) after the \d+ in the first match?
Answer: we could but it is not necessary. The \d specifier only matches 0-9 - so as soon as it hits a non digit it stops anyway.
What do the opening and closing '/' mean
Answer: they are just regular expression delimiters saying where it starts and where stops.

Now for the replace - easy
\1 means the first saved match - (@\d+)
| - insert a | - we don't need to escape it because in the replace string the | character is just a |
\2 - the second matched parameter (.*?@)
Julian HansenCommented:
You are welcome.
steamngnAuthor Commented:
I went round and round ALL DAY on this.... and now with your insight I see where I was going wrong!
Excellent help, Thank you
Julian HansenCommented:
Again you are welcome Andy - glad I could be of assistence.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.