What PCRE will deliver a sub-string if present, else null?
You'll see that formulation has attracted answers mostly that solve just the example, rather than the general requirement.
What PCRE will deliver a sub-string if present, else null?
You'll see that formulation has attracted answers mostly that solve just the example, rather than the general requirement.
Just explaining what you're looking to accomplish should probably be your first option:
I'm interested in how to accomplish this for the general case described; the example above is meant only as a simple illustration of what I'm looking to accomplish in less trivial cases.
Yes, this might have been a reasonable interpretation of your question without having to explicitly state it - but clearly, this was not the case for several readers. So I've added this paragraph to your question.
Questions (and answers) should be as long as necessary and no longer - but when in doubt, err on the side of over-communicating. It's always possible for you or someone else to remove superfluous detail later, but rather more difficult to add it.
I attempted an answer to your question, but it's difficult to know if I was successful. The problem for me is that what you want to accomplish seems trivially easy in whatever general purpose language you are calling PCRE from, but (perhaps) impossible within the regex itself. A regex engine is not Turing complete. That means there are some problems they cannot solve. And many other problems they can solve only with great difficulty and complication.
All this leads me to believe that your question suffers from the XY Problem. Your example is easy enough to solve (and the first two answers did solve the original version of the question). But you probably don't actually care about finding 'there' in the string 'hello there dolly'. So the trivial case is a distraction. You'd be better off describing in more detail what you are actually trying to accomplish. What do the strings actually look like? Are there regular patterns you can use to your advantage? Might you be better off using something other than a regex?
It probably would help if you'd tagged the question with whatever language you are using to wrap PCRE. Shog9 added perl, but I don't think you are really using Perl. That makes my answer less useful than it could be.
The reason that you're not getting answers you want is because your question was not as high a quality as it could have been. You could read the how to ask page for help with asking questions that will get you better answers.
But, as it stands, your question fits three categories for closure:
Unclear what you're asking: You have a random "EDIT:" in the middle of a sentence, which is not required or recommended. Also, your question can't stand on it's own without the example, so it makes sense that people turn to the example.
No clear problem statement: You just provide what you want. You don't show an attempt or any problems that you faced.
Too broad: You don't show any attempt or research effort. Also, since you mention that answering your example is not the full extent, that is even more reason for it to be closed as too broad.