8
\$\begingroup\$

What is the shortest regular expression equivilant to the following?

ENERO|GENER|GENNAIO|IANUARI|IANUARII|IONAWR|JAANUARIL|JANAIRU|JANAR|JANNEWARIS|JANUAR|JANUARI|JANUARIE|JANUARO|JANUARY|JANUWALE|JANVIER|JANVYE|JENUWARI

The shortest regex, in bytes, wins.

If you are worried about what flavor of regex we are using, then be the first one to edit my question, and you can have a link to a description of the specific regex flavor of your choice. Please choose a mainstream regex flavor, nothing too esoteric. * should mean zero or more. + should be one or more and so on and so forth.

\$\endgroup\$
7
  • 14
    \$\begingroup\$ That way of choosing a regex flavor seems really weird; why not just allow all regex flavors? Answers could then just specify in the header which flavor they're using (this also makes the competition more interesting, since people have a broader range of features available) \$\endgroup\$ Jun 6 at 4:24
  • 1
    \$\begingroup\$ What do you mean by "handful"? \$\endgroup\$ Jun 6 at 6:06
  • 4
    \$\begingroup\$ @UndoneStudios they mean the ones in the question \$\endgroup\$
    – OrangeDog
    Jun 6 at 15:35
  • \$\begingroup\$ @RydwolfPrograms We have to hold the regex flavor constant, otherwise you could invent your own flavor of regex which is simply J. Given that J is one character, you would probably win the competition. \$\endgroup\$ Jun 7 at 1:01
  • 6
    \$\begingroup\$ @SamuelMuldoon We already have rules that take care of this problem. Scoring is per-language here; an answer in PCRE doesn't compete with one in g/re/p regex, only other PCRE answers. So a single-byte answer in a custom flavor only wins for that flavor, and is thus pointless. \$\endgroup\$ Jun 7 at 3:20

5 Answers 5

6
\$\begingroup\$

Python re module, 104 110 bytes

-6 based on Neil's comment

JENUWARI|JA(N(VYE|VIER|UWALE|UAR(IE?|Y|O)?|NEWARIS|AIRU|AR)|ANUARIL)|IONAWR|IANUARII?|GEN(NAIO|ER)|ENERO
\$\endgroup\$
1
  • 5
    \$\begingroup\$ |AIRU|AR)| is a byte shorter than |A(IRU|R))|. (I think the same idea applies in a few other places as well.) Similarly, |Y|O is a byte shorter than |[YO]. \$\endgroup\$
    – Neil
    Jun 6 at 7:31
4
\$\begingroup\$

104 bytes

ENERO|GEN(ER|NAIO)|IANUARII?|IONAWR|JAANUARIL|JAN(AIRU|AR|NEWARIS|UAR(IE?|O|Y)?|UWALE|VIER|VYE)|JENUWARI

Should work with any most regex styles, since I only use |?(), but below is a TIO for Java:

Try it online.

The last portion of the regex above could be one of these three for the same byte count:

  • JAANUARIL|JAN(AIRU|AR|NEWARIS|UAR(IE?|O|Y)?|UWALE|VIER|VYE)|JENUWARI (the one used above)
  • JA(ANUARIL|N(AIRU|AR|NEWARIS|UAR(IE?|O|Y)?|UWALE|VIER|VYE))|JENUWARI (JA combined for suffix of two options)
  • J(AANUARIL|AN(AIRU|AR|NEWARIS|UAR(IE?|O|Y)?|UWALE|VIER|VYE)|ENUWARI) (J combined for suffix of three options)

Very curious if <104 bytes is even possible at this point. 🤔

†: Apparently not all. Thanks for the info @ikkachu.

\$\endgroup\$
2
  • 3
    \$\begingroup\$ not any regex style: standard POSIX basic REs (as used by e.g. sed and grep without the -E options) require \(/\) for grouping, don't have ? (but \{0,1\} does the same) and have no syntax for alternation at all. \$\endgroup\$
    – ilkkachu
    Jun 6 at 18:46
  • 1
    \$\begingroup\$ @ilkkachu Ah ok, that's good to know. I don't know too much about all the different regex flavors I must admit. Thanks for the info. \$\endgroup\$ Jun 6 at 18:55
3
\$\begingroup\$

110 bytes

J(A(N(U(AR([OY]|IE?)?|WALE)|V(IER|YE)|A(IRU|R)|NEWARIS)|ANUARIL)|ENUWARI)|I(ANUARI?I|ONAWR)|GEN(NAIO|ER)|ENERO

Which may be more easily read with designified whitespace like this:

J(
    A(
        N(
            U(
                AR(
                    [OY]
                   | IE?
                )?
              | WALE
            )
            | V(IER | YE)
            | A(IRU | R)
            | NEWARIS
        )
        | ANUARIL
    )
  | ENUWARI
)
| I(ANUARI?I | ONAWR)
| GEN(NAIO   | ER)
| ENERO

This pattern works both in engines guaranteed to select the leftmost valid alternative in an A|B|C set, as well as in engines guaranteed to choose the longest valid alternative overall from that set.

Because of its alternation of literal strings, this pattern runs faster in engines with the Aho–Corasick trie optimization than in those lacking that feature.

\$\endgroup\$
1
  • \$\begingroup\$ I'm guessing that you could make the same savings as I suggested for @RootToo's answer. \$\endgroup\$
    – Neil
    Jun 6 at 23:26
1
\$\begingroup\$

104 bytes

ENERO|GEN(NAIO|ER)|IANUARII?|IONAWR|JA(ANUARIL|N(AIRU|AR|NEWARIS|UAR(IE?|O|Y|)|UWALE|VIER|VYE))|JENUWARI

Or, formatted more nicely (with insignificant whitespace):

ENERO |
GEN(
    NAIO |
    ER
) |
IANUARI I? |
IONAWR |
JA(
    ANUARIL |
    N(
        AIRU |
        AR |
        NEWARIS |
        UAR(
            I E? |
            O |
            Y |
            [the empty string]
        ) |
        UWALE |
        VIER |
        VYE
    )
) |
JENUWARI
\$\endgroup\$
0
\$\begingroup\$

104 bytes

ENERO|GEN(ER|NAIO)|IANUARII?|IONAWR|JA(ANUARIL|N(UWALE|VIER|VYE|UAR(IE?|O|Y)?|NEWARIS|AIRU|AR))|JENUWARI

Spent way too long trying to find a shorter solution and am convinced it doesn't exist.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.