Can you determine Success/Failure of Back-Referenced Groups without matching the text?


Is there a way to determine if a back-referenced group captured successfully, but WITHOUT trying to match the actual text?

For instance, an email address can be written two ways:


Larry Q. Smith III <>

What I'm hoping to do is put an Optional Group around the
"Larry Q. Smith III <" part and another Optional Group around the last character ">".

The problem is that I don't want to capture things like:
Larry Q. Smith III <


--- I was hoping for a way to determine if the first group matched successfully, and if that's true, then the last > MUST be there.

I was hoping NOT to use alteration as this can be very expensive with backtracking.

I'd appreciate any help anyone has to offer.

Thank you!
Who is Participating?
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.

Terry WoodsIT GuruCommented:
> I was hoping NOT to use alternation as this can be very expensive with backtracking.

The first thing that comes to mind is: is this really a problem? Unless you are pushing the limits of performance, or some other performance related bottleneck or cost, the labour incurred in finding a faster solution can often cost more than just running a sub-optimal pattern.

However, I'll have a think about the actual question now...
Terry WoodsIT GuruCommented:
By the way, can you please share which language/tool you are going to run the regex on? This makes a difference as to whether it might be possible.
Terry WoodsIT GuruCommented:
This seems to work for me:

The (?(1)>|) means match > if the first captured subpattern matched, otherwise match nothing.

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
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Terry WoodsIT GuruCommented:
My pattern for the email address is this part:

You may have a better pattern for that bit, as I threw mine together in a hurry
kaufmed 👽Commented:
If your language supports it, you might throw the first part of Terry's pattern inside of an atomic group to mitigate backtracking:


Open in new window

LDawggieAuthor Commented:
Thank you Terry for reminding me of the (?(1)then|else) syntax.
I must have missed that in the Regular Expressions book. :)

Thank you Kaufmed for your advice about the atomic grouping. That's a good idea!
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
Regular Expressions

From novice to tech pro — start learning today.