easy regexp by tabasa

li=gets\x0d
ans=[]\x0d
\x0d
def Or a\x0d
 ans=[]\x0d
 ans<<a.split("|")\x0d
 ans\x0d
end\x0d
\x0d
def hate a\x0d
 ans=[""]\x0d
 if a.class==String\x0d
  ans <<a\x0d
 else\x0d
  a.each{|i|\x0d
   ans<<i\x0d
  }\x0d
 end\x0d
 ans\x0d
end\x0d
\x0d
def chars a\x0d
 dat=[]\x0d
 a.each_byte{|i|\x0d
  dat<<i.chr\x0d
 }\x0d
 dat\x0d
end\x0d
\x0d
last=""\x0d
len =li.length\x0d
i =0\x0d
c=""\x0d
hatef=0\x0d
while i<len\x0d
 c=li[i,1]\x0d
 if c=='?'\x0d
  hatef=1\x0d
  ans << hate(last) \x0d
 else\x0d
  hatef=0\x0d
  ans << last if last !='?' && i>0\x0d
  if c=='('\x0d
   /\(([^)]+)\)/ =~ li[i..-1]\x0d
   last= Or($1)\x0d
   i += $1.length+2\x0d
   next\x0d
  end\x0d
  if c=='['\x0d
   /\[([^\]]+)\]/ =~ li[i..-1]\x0d
   last = chars $1\x0d
   i += last.length+2\x0d
   next\x0d
  end\x0d
 end\x0d
 last =c\x0d
 i+=1\x0d
end\x0d
ans <<last if hatef==0\x0d
\x0d
def put an,a,l,n\x0d
 if l==n\x0d
  puts an\x0d
 else\x0d
  if a[n].class==String\x0d
   an<<a[n] \x0d
   put an,a,l,n+1\x0d
  else\x0d
   a[n].each{|i|\x0d
    if i.class==String\x0d
     put an+i,a,l,n+1 \x0d
    elsif i.class==Array\x0d
     i.each{|j|put an+j,a,l,n+1}\x0d
    end\x0d
   }\x0d
  end\x0d
 end\x0d
end\x0d
\x0d
put("",ans,ans.length,0)

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page