import re

# OEIS Internal Format parsing
id = re.compile(r'^%I (A\d+) ?([MN]\d+)? ?([MN]\d+)?', re.M)
unsigned = re.compile(r'^%[STU] A\d+ ((?:\d+,?)*)', re.M)
signed = re.compile(r'^%[VWX] A\d+ ((?:-?\d+,?)*)', re.M)
name = re.compile(r'^%N A\d+ ([^.:]+)([^.]*)', re.M)
reference = re.compile(r'^%D A\d+ (.+).?', re.M)
link = re.compile(r'^%H A\d+ (.*)<a href="(.*)">(.*)</a>(.*)', re.M)
formula = re.compile(r'^%F A\d+ (.+)', re.M)
cross_reference = re.compile(r'^%Y A\d+ (.+)\.?', re.M)
author = re.compile(r'^%A A\d+ (.+)', re.M)
offset = re.compile(r'^%O A\d+ (-?\d+), ?(-?\d+)', re.M)
maple = re.compile(r'^%p A\d+ (.+)', re.M)
mathematica = re.compile(r'^%t A\d+ (.+)', re.M)
other_programs = re.compile(r'^%o A\d+ (.+)', re.M)
error = re.compile(r'^%E A\d+ (.+)\.?', re.M)
example = re.compile(r'^%e A\d+ (.+)', re.M)
keywords = re.compile(r'%K A\d+ [a-zA-Z]+,?', re.M)
comment = re.compile(r'%C A\d+ (.+)', re.M)
blank_line = re.compile(r'^\n\s*', re.M)
showing_line = re.compile(r'Showing \d+-\d+ of (\d+)')

# Maths expression parsing
valid = re.compile(r'^[()^+-/*!n\d]+$')
parens = re.compile(r'\([+-/n\d*!^]+\)')
operands = re.compile(r'(\d+|n)')
fac = re.compile(r'(\(?[+\d*-/n^]+\)?)!')
opp_paren = re.compile(r'\)\(')
paren_fac = re.compile(r'\)fac')
expo = re.compile(r'\^')
