-2

Conditional pattern as described here doesnt seem to work

See my current setup

I am trying to match any line that contains test and pie, any ideas what I am doing wrong?

1 Answers1

-1

My guess is that you might be trying to design some expression that might look like:

(?(exp)(?P<exp>)\btest\b|(?P<fruits>)\bpie\b)

Demo 1

or

(?(1)(\btest\b)|(\bpie\b))

Demo 2


The expression is explained on the top right panel of regex101.com, if you wish to explore/simplify/modify it, and in this link, you can watch how it would match against some sample inputs, if you like.

Emma
  • 27,428
  • 11
  • 44
  • 69
  • 3
    It would be better to explain things in your own words, otherwise, somebody might think you're not making sense. Like for instance, unless this sequence `(?(exp)(?P)\btest\b|(?P)\bpie\b)` is run inside a quantified group, `(?(exp))` is always false, and the _yes_ part is never matched. Also, even though PCRE, like Perl respects Pythons capture group naming convention, it's not the Perl like syntax. –  Jul 30 '19 at 23:29
  • `(?(1)(\btest\b)|(\bpie\b))` is what I was looking for, thanks. Is there any way to do this without a conditional? – sp00kyb00g13 Jul 31 '19 at 12:49
  • 2
    @sp00kyb00g13 `(?(1)(\btest\b)|(\bpie\b))` is a pattern that is equal to `pie`. Why do you need such a strange pattern? – Wiktor Stribiżew Jul 31 '19 at 12:52
  • @WiktorStribiżew after looking at it more that was not what I was looking for, it just matches for pie as you said. – sp00kyb00g13 Jul 31 '19 at 13:00