Is there a way to combine two words with one word having 'p' has the start, and the other having 'p' as the end, technically

Viewed 51

I've been wondering about this for a long, long time.

Note: This is not useful in anyway unless it is said to be useful by someone... But, I am just wondering if you can write this regex in a weird complicated way:

/^(webp|p(?:ng|hotoshop))$/gm

Can you combine the two 'p's for web'p' and 'p'ng/hotoshop?

Remember it should only match:

webp
png
photoshop

I just couldn't find anything related to this, and I have no idea what to google to know about this.

To my understanding, this must not be possible in regex unless, this happens, ofcourse the below doesn't happen unless you mess with your fonts, really:
regex fun

1 Answers

In those regex flavors that support conditional regex constructs, you can use

^(web)?p(?(1)$|(?:ng|hotoshop))$

See the regex demo.

Details

  • ^ - start of string
  • (web)? - Group 1 (optional): - web string, 1 or 0 times
  • p - a p
  • (?(1)$|(?:ng|hotoshop)) - If Group 1 matched, match the end of string, else, match ng or hotoshop
  • $ - end of string.
Related