def pinyin_regex_pattern(istream=None):
    """ Returns a pinyin regex pattern, with optional tone number. """
    all_pinyin = get_all_pinyin(istream)

    # Sort from longest to shortest, so as to make maximum matches whenever
    # possible.
    all_pinyin = sorted(all_pinyin, key=len, reverse=True)

    # Build a generic pattern for a single pinyin with an optional tone.
    pattern = '(%s)([0-5]?)' % '|'.join(all_pinyin)

    return pattern