2

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.

5
  • I wish one could figure what the question was. (Take a break: Consider that the question was posted by somebody else, try figuring it; you'd perhaps realize that the question isn't quite clear.)
    – 0xdeadbeef
    Commented Aug 17, 2014 at 15:09
  • 2
    What the hell is a PCRE?
    – Robert Harvey Mod
    Commented Aug 17, 2014 at 15:47
  • 2
    @RobertHarvey: PCRE - Perl Compatible Regular Expressions
    – peterSO
    Commented Aug 17, 2014 at 16:09
  • 4
    Seems to me, then, that the question would benefit from a [perl] tag.
    – Robert Harvey Mod
    Commented Aug 17, 2014 at 16:15
  • If you want a more general solution it may help to give several examples with the desired results for each. Commented Aug 17, 2014 at 19:34

3 Answers 3

9

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.

3
  • I have tried this approach, and got responses like, "Provide some real code" or "I cant see what your code does" when explaining in very simple terms what i am trying to accomplish.
    – Ricky
    Commented Oct 22, 2016 at 2:22
  • "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." I rather think it was not the case for readers mostly unqualified to answer the question. Those aren't the kind of readers from whom I'm most interested in attracting answers.
    – ChrisJJ
    Commented Dec 5, 2016 at 23:53
  • Wow, that's a heck of a delayed response, @Chris... Anyway, my point stands: if folks aren't picking up on your intentions, make 'em more explicit. Prior to my edit, you appear to have attracted answers that took the question literally; if that wasn't your intent, you might've saved time by just stating what you were after.
    – Shog9 Mod
    Commented Dec 6, 2016 at 0:03
2

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 , but I don't think you are really using Perl. That makes my answer less useful than it could be.

1
  • Jon, what I am actually trying to accomplish /is/ the general case. I know solutions to many specific cases, and I'd rather not add to the list :) Also, I am looking for a solution in PCRE independent of the outer language. I do no know how the editor who tagged Perl thought this was useful. PCRE said it all. Thanks.
    – ChrisJJ
    Commented Aug 18, 2014 at 22:10
0

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
  • no clear problem statement
  • too broad

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.

8
  • "You have a random edit in the middle of a sentence" What makes you think it is random?
    – ChrisJJ
    Commented Aug 17, 2014 at 16:19
  • Well what I'm saying is that the word "EDIT" is not part of the sentence. It's just thrown in there.
    – Anonymous
    Commented Aug 17, 2014 at 16:20
  • "No clear problem statement" was definitely an issue; as far as I can tell, this is a superset of all other major problems with the question. I'm thinking "why are you asking this question?" should probably be in the on-page asking help.
    – Shog9 Mod
    Commented Aug 17, 2014 at 19:45
  • @Shog9 I believe it's already there: "Explain how you encountered the problem you're trying to solve, and any difficulties that have prevented you from solving it yourself," but it could sure could be beneficial to add a "No coding requests" section.
    – Anonymous
    Commented Aug 17, 2014 at 20:09
  • I'm talking about /questions/ask itself.
    – Shog9 Mod
    Commented Aug 17, 2014 at 20:14
  • @Anonymous, "no clear problem statement", I'd say. Thanks anyway for trying.
    – ChrisJJ
    Commented Aug 17, 2014 at 21:26
  • Why so offensive. You didn't even answer a question.
    – Kubuxu
    Commented Aug 17, 2014 at 23:11
  • @Kubuxu I wasn't trying to be offensive. I just wanted to make it clear that it would have been better if more effort was put in to reading Stack Overflow guides for asking questions. But, I can see how that may be interpreted, so I edited it. As for not answering the question, I believe quality questions will get more appropriate answers and once the quality of a question is increased, good answers will follow. I also explain "Also, your question can't stand on it's own without the example, so it makes sense that people turn to the example." But, if you don't agree, that's perfectly fine.
    – Anonymous
    Commented Aug 18, 2014 at 0:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .