From 1a8f225b93bc215d0780269824e2ab6f6faf4b27 Mon Sep 17 00:00:00 2001 From: j Date: Tue, 29 Apr 2008 15:34:27 +0200 Subject: [PATCH] make findRegexp do more --- oxutils/text.py | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/oxutils/text.py b/oxutils/text.py index 8c54a3d..157e6c5 100644 --- a/oxutils/text.py +++ b/oxutils/text.py @@ -5,18 +5,35 @@ import re 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 = ''): - string0 = re.escape(string0) +def findString(string, string0='', string1 = ''): + """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: string1 = re.escape(string1) else: string1 = '$' - result = findRegexp(string, string0 + '(.*?)' + string1) - if result: - return result[0].strip() - return None + return findRegexp(string, string0 + '(.*?)' + string1) # Capitalizes the first letter of a string. capfirst = lambda x: x and x[0].upper() + x[1:] @@ -158,3 +175,4 @@ def smartSplit(text): yield "'" + bit[1:-1].replace("\\'", "'").replace("\\\\", "\\") + "'" else: yield bit +