Regex, Paper, Scissors, Lizard, Spock

Warm up: Regex, Paper, Scissors

This is the challenge I originally wanted to post, before realising that some very short solution exist. Nevertheless, it can be an interesting problem to think about in preparation for the actual challenge below.

Write three regexes R, P and S such that they match each other in a cyclic Rock, Paper, Scissors fashion. In particular, R matches S, S matches P and P matches R, but R doesn't match P, S doesn't match R and P doesn't match S. Here's a handy table:

Regex   Matches   Doesn't match
R       S         P
P       R         S
S       P         R


It doesn't matter what R, P and S do on any other inputs, including themselves.

Here, match just means that some (possibly empty) substring of the input is matched. The match does not need to cover the entire input.

The challenge: Regex, Paper, Scissors, Lizard, Spock

For this challenge, you'll solve a tougher version of the above problem, based on the RPS variant Rock, Paper, Scissors, Lizard, Spock (as popularised by The Big Bang Theory). In RPSLV, there are five different symbols, that beat each other in two cycles:

• Rock → Scissors → Lizard → Paper → Spock → Rock
• Rock → Lizard → Spock → Scissors → Paper → Rock

You should write five regexes R, P, S, L and V which mimic this structure when given to each other as input. Here is the corresponding table:

Regex   Matches   Doesn't match
R       L, S      V, P
L       V, P      S, R
V       S, R      P, L
S       P, L      R, V
P       R, V      L, S


Just to be clear, you should not match the string R, P, etc, but the other regexes. E.g. if your regex R is ^\w$ for example, then P and V have to match the string ^\w$, whereas S and L shouldn't.

Again, match just means that at least one (possibly empty) substring of the input is matched. The match does not need to cover the entire input. For example \b (word boundary) matches hello (at the beginning and at the end), but it doesn't match (^,^).

You may use any regex flavour, but please state the choice in your answer and, if possible, provide a link to an online tester for the chosen flavour. You may not use any regex features that let you invoke code in the flavour's host language (like the Perl flavour's e modifier).

Delimiters (like /regex/) are not included in the regex when given as input to another, and you cannot use modifiers that are outside of the regex. Some flavours still let you use modifiers with inline syntax like (?s).

Your score is the sum of the lengths of the five regexes in bytes. Lower is better.

It turns out to be a lot simpler to find a working solution to this problem than it may seem at first, but I hope that finding an optimal solution is quite tricky.

• Does, say, R have to match the entire regex of S or a substring of S, provided it does not match any substrings of P or V? – Okx Jul 5 '17 at 13:00
• @Okx "match just means that at least one (possibly empty) substring of the input is matched. The match does not need to cover the entire input. For example \b (word boundary) matches hello (at the beginning and at the end), but it doesn't match (^,^)." – Martin Ender Jul 5 '17 at 13:01
• Presumably it doesn't matter if a regex matches itself? – Brilliand Jul 5 '17 at 18:57
• @Brilliand Correct. – Martin Ender Jul 5 '17 at 19:07
• Great puzzle. I've created an interactive version here, if anyone is interested: shark.fish/rock-paper-scissors – shark.dp Jul 30 '17 at 11:22

PCRE .NET, 35 32 bytes

-3 bytes thanks to Martin Ender

Rock:

([*?]$)  Paper: [)$]$+  Scissors: [+?]$.*


Lizard:

[+$]$.?


Spock:

[*)]$ The idea here is to match characters at the end of other regexes which are reserved regex characters, but stop being treated as such when inside a character class. • Okay, you win :P – ETHproductions Jul 5 '17 at 13:33 • Sneaky use of putting things after the EOL "$" that become ignored when actively used and matchable when passively used – Stilez Jul 5 '17 at 13:56

PCRE, 15 14 bytes

Rock:
B

Paper:
\b$ Scissors: b|B. Lizard: \B. Spock: ^\w • Very impressive. – Eric Duminil Jul 8 '17 at 21:16 • Unbeatable! I've been fiddling with Rock = Q (one 14b solution exists with Lizard = \Q\, then the rest similar to yours) but to absolutely no avail. – jaytea Oct 22 '17 at 9:44 no fancy features, 35 30 bytes 5 bytes saved by Neil's idea which uses that ] needs no \. This works for example with the python re module. R='[SLR]]' P='[RVP]]' S='[PLS]]' L='[PVL]]' V='[SRV]]'  It searches for a ] preceded by a letter that indicates which rule it is. Previous version used R='\[[RSL]' etc. An earlier attempt with score 40 was using R='[SL]x|Rx' etc. • Save 5 bytes by reversing everything: R='[LSR]]' etc. – Neil Jul 5 '17 at 15:24 • @Neil That's a great improvement, thanks! – Christian Sievers Jul 5 '17 at 17:23 • This works with python's re well, Python should probably be the header then – cat Jul 5 '17 at 17:27 • @cat I also wrote "for example", the whole sentence is just to say something concrete that I actually tried. I guess I could say POSIX like some others did, but I think my header is quite correct. – Christian Sievers Jul 5 '17 at 17:48 PCRE, 20 19 Rock W  Paper ^\w  Scissors ^\W  Spock w?\x57$


Lizard

[w]W?


20 bytes

R = 'R|VP'
L = 'L|RS'
V = 'V|LP'
S = 'S|RV'
P = 'P|LS'

• Oh wow, it can be so easy! – Christian Sievers Jul 6 '17 at 22:31
• That's beautiful. – Eric Duminil Jul 8 '17 at 21:09

JavaScript, 45 bytes

Another trivial solution.

R:
^R|^.[SL]
P:
^P|^.[RV]
S:
^S|^.[PL]
L:
^L|^.[PV]
V:
^V|^.[SR]

• Oh just realised my answer is a longer/similar version of yours, want me to delete it? – TheLethalCoder Jul 5 '17 at 13:22
• @TheLethalCoder All the answers currently are just longer/shorter versions of each other :p – dzaima Jul 5 '17 at 13:25
• I suppose haha... – TheLethalCoder Jul 5 '17 at 13:26

POSIX, 50 45 bytes

Rock
.{5}RP?V?
Paper
.{5}PS?L?
Scissors
.{5}SR?V?
Lizard
.{5}LR?S?
Vulcan (Spock)
.{5}VP?L?


Could be done shorter but the (hide matches after $) trick got used, so I'm looking for another way The first 5 chars of each string are ignored when matching. So the effective target string simplifies to just X?Y?. None of them have any double letters because "?" is an ordinary char, so the last 4 chars when used as a regex have to match (null string). So the patterns collapse down to "contains 5 chars followed by the target letter": meaning chars 6-9 of the target must contain the target letter (the 5th char in each string) Update: 35 byte version below, now! • I always thought the V isn't actually for Vulcan but to represent the shape of the Vulcan salute (which is the hand gesture you use to represent Spock when playing RPSLV in person). – Martin Ender Jul 5 '17 at 15:36 • Anyway, welcome to PPCG! Nice first answer. I like that you've inverted the logic compared to all existing answers (matching only one letter and putting the letters beating the current regex into the regex). – Martin Ender Jul 5 '17 at 15:40 • Does POSIX automatically anchor the match to the beginning of the string? Otherwise, couldn't you drop those commas? – Martin Ender Jul 5 '17 at 15:41 • I think its POSIX. Could be perc. But yes, nicely spotted! 5 chars less! – Stilez Jul 5 '17 at 15:43 • There's no need to compete with Jelly. Just pick a language you like and enjoy yourself. :) – Martin Ender Jul 5 '17 at 15:59 Vanilla RE, 40 characters Not the most concise or elegant solution but has a pleasing quasi-semantic visual structure! [^r][sl] [^p][vr] [^s][lp] [^l][pv] [^v][rs]  Rock beats Scissors or Lizard Paper beats Vulcan or Rock Scissors beats Lizard or Paper Lizard beats Paper or Vulcan Vulcan beats Rock or Scissors PCRE, 65 bytes This is a really trivial solution - and not very clever at all - but I'll try to golf it. V: (?#V).+[SR]\)  L: (?#L).+[PV]\)  S: (?#S).+[PL]\)  P: (?#P).+[RV]\)  R: (?#R).+[SL]\)  Essentially, each regex has an 'identifier', in the form of a comment, which tells the other regexes whether it should be matched or not. .NET, 50 bytes In order they are R, P, S, L, V. [^R]\^[SL] [^P]\^[RV] [^S]\^[PL] [^L]\^[PV] [^V]\^[SR]  Works by looking for the identifier group (for example, [^R]) in each of the other expressions. Changing the expressions to ^R|\^[SL], or similar, seems to work but then it's a bit too similar to @dzaima's answer although it would get it to 45 bytes. POSIX, 35 bytes Rock R?^[LS] Paper P?^[RV] Scissors S?^[LP] Lizard L?^[PV] Vulcan (Spock) V?^[RS]  A completely different way to "hide" behind a start/end symbol, so I feel OK about it :) I'm matching to start because "?" would always have to go between letter and end/$ if done the other way.

10 bytes less than my 1st solution, and conceptually simple which is a bonus I like.