Skip to content

Instantly share code, notes, and snippets.

@hidsh
Created February 21, 2012 05:49
Show Gist options
  • Save hidsh/1874039 to your computer and use it in GitHub Desktop.
Save hidsh/1874039 to your computer and use it in GitHub Desktop.
python tiny regular expression utility which is aimed to be re-builder on emacs
# reb.py - tiny regular expression utility which is aimed to be re-builder
#
# NOTE: on IDLE, you can copy'n paste following lines for ease to use.
""" -- FOR IDLE --
import sys
sys.path.append('d:\\g\\py')
from reb import reb
"""
import re
class t_reb():
def __init__(self):
self.__line = ''
self.__re_str = ''
def help(self):
print """usage:
reb.help() print this usage
reb.go() interactive set and search loop (Ctrl-C --> quit)
reb.values() show current regexp and line"""
def __search(self):
rek = re.compile(self.__re_str)
m = rek.match(self.__line)
if m:
print ' matched'
for i, x in zip(range(1, len(m.groups())+1), m.groups()):
print ' group(%d): \'%s\'' % (i, x)
else:
print ' not match'
def __re(self, s):
self.__re_str = s
self.__search()
def __line(self, s):
self.__line = s
self.__search()
def values(self):
print 're :\'%s\'' % self.__re_str
print 'line:\'%s\'' % self.__line
def __i(self):
ins = raw_input('re :')
if ins != '': self.__re_str = ins
ins = raw_input('line:')
if ins != '': self.__line = ins
self.__search()
def go(self):
while 1:
try:
ins = raw_input('re [%s]:' % self.__re_str)
if ins != '': self.__re_str = ins
ins = raw_input('line [%s]:' % self.__line)
if ins != '': self.__line = ins
self.__search()
except KeyboardInterrupt: break
reb = t_reb()
reb.help()
reb.go()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment