make findRegexp do more

This commit is contained in:
j 2008-04-29 15:34:27 +02:00
parent d5d58dba34
commit 1a8f225b93

View file

@ -5,18 +5,35 @@ import re
def findRegexp(string, regexp): def findRegexp(string, regexp):
return re.compile(regexp, re.DOTALL).findall(string) result = re.compile(regexp, re.DOTALL).findall(string)
if result:
return result[0].strip()
return None
def findString(string, string0, string1 = ''): def findString(string, string0='', string1 = ''):
string0 = re.escape(string0) """Return the string between string0 and string1.
If string0 or string1 is left out, begining or end of string is used.
>>> findString('i am not there', string1=' not there')
'i am'
>>> findString('i am not there', 'i am ', ' there')
'not'
>>> findString('i am not there', 'i am not t')
'here'
"""
if string0:
string0 = re.escape(string0)
else:
string0 = '^'
if string1: if string1:
string1 = re.escape(string1) string1 = re.escape(string1)
else: else:
string1 = '$' string1 = '$'
result = findRegexp(string, string0 + '(.*?)' + string1) return findRegexp(string, string0 + '(.*?)' + string1)
if result:
return result[0].strip()
return None
# Capitalizes the first letter of a string. # Capitalizes the first letter of a string.
capfirst = lambda x: x and x[0].upper() + x[1:] capfirst = lambda x: x and x[0].upper() + x[1:]
@ -158,3 +175,4 @@ def smartSplit(text):
yield "'" + bit[1:-1].replace("\\'", "'").replace("\\\\", "\\") + "'" yield "'" + bit[1:-1].replace("\\'", "'").replace("\\\\", "\\") + "'"
else: else:
yield bit yield bit