#!/opt/perl/bin/perl

use 5.016;

use strict;
use warnings;
no  warnings 'syntax';
use feature  'current_sub';
use feature  'signatures';
no  warnings 'experimental::signatures';



#
# Given a list of digits, return a character class matching them
#
sub character_class (@list) {
    my %seen;
    @list = sort {$a <=> $b} grep {!$seen {$_} ++} @list;

    if (@list == 0) {return ""}
    if (@list == 1) {return $list [0]}
    my $out = "[";
    for (my $i = 0; $i < @list; $i ++) {
        my $j = $i + 2;
        if ($j < @list && $list [$j] - $list [$i] == 2) {
            while ($j + 1 < @list &&
                            $list [$j + 1] - $list [$i] == $j + 1 - $i) {
                $j ++;
            }
            $out .= $list [$i] . "-" . $list [$j];
            $i = $j;
        }
        else {
            $out .= $list [$i];
        }
    }
    $out .= "]";
    return $out;
}


#
# Given a list of numbers (all of the same length), return a regular
# expression matching them
#
sub condense (@list) {
    my %seen;
    @list = sort {$a <=> $b} grep {!$seen {$_} ++} @list;

    return "" unless @list;
    return $list [0] if @list == 1;

    my $length = length ($list [0]);
    for (my $i = 1; $i < @list; $i ++) {
        die "All elements must be of the same length (@list)\n"
             unless length $list [$i] == $length;
    }

    #
    # Is there a common prefix?
    #
    my $common_prefix = $list [0];
    for (my $i = 0; $i < @list && length $common_prefix; $i ++) {
        my $c = 0;
        $c ++ while $c < length $common_prefix &&
                    substr ($common_prefix, 0, $c + 1) eq
                    substr ($list [$i], 0, $c + 1);
        $common_prefix = substr $common_prefix, 0, $c;
    }

    #
    # Chop off the common prefix
    #
    if (length $common_prefix) {
        my $c = length $common_prefix;
        foreach my $elem (@list) {
            substr ($elem, 0, $c) = "";
        }
    }

    #
    # If what's left, is one character wide, return a character class
    #
    if (length ($list [0]) == 1) {
        return $common_prefix . character_class @list;
    }

    #
    # Else, bucketize on first character, recurse and combine.
    #
    my %buckets;
    foreach my $elem (@list) {
        my ($head, $tail) = $elem =~ /^(.)(.+)$/s;
        die "\$elem = $elem" unless defined $head;
        push @{$buckets {$head}} => $tail;
    }

    my @clauses;

    foreach my $head (sort {$a <=> $b} keys %buckets) {
        my $pattern = &{+__SUB__} (@{$buckets {$head}});
        push @clauses => "$head$pattern";
    }

    local $" = "|";
    return "$common_prefix(?:@clauses)";
}


my @list = <ARGV>;
chomp @list;

say condense @list;


__END__
