Python:Extra string functions: before, after, between

Fantastic! Python now has the built-in partition function. They should have gone the whole hog and implement the simpler ones, before after and between.

?Download strlib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
strlib.py
"""
 
def after(s,pat):
    #returns the substring after pat in s.
    #if pat is not in s, then return a null string!
    pos =s.find(pat)
    if pos != -1:
       return s[pos+len(pat):]
    return ""
 
 
def rafter(s,pat):
    #returns the substring after pat in s.
    #if pat is not in s, then return a null string!
    pos =s.rfind(pat)
    if pos != -1:
       return s[pos+len(pat):]
    return ""
 
 
def before(s, pat):
    #returns the substring before pat in s.
    #if pat is not in s, then return a null string!
    pos =s.find(pat)
    if pos != -1:
       return s[:pos]
    return ""
 
 
def rbefore(s, pat):
    #returns the substring before pat in s.
    #if pat is not in s, then return a null string!
    pos =s.rfind(pat)
    if pos != -1:
       return s[pos+len(pat):]
    return ""
 
 
def between(s, bpat, apat):
    # returns substring between bpat and apat in s.
    # if bpat or apat is not in s, then return a null string.
    start = s.find(bpat)
    if start != -1:
       end = s[start+len(bpat):].find(apat)
       if end != -1:
          return s[start+len(bpat):start+len(bpat)+end] 
       return ""
    return ""
 
def rbetween(s, bpat, apat):
    # returns substring between bpat and apat in s.
    # if bpat or apat is not in s, then return a null string.
    end  = s.rfind(apat)
    if end != -1:
       before= s[:end].rfind(bpat)
       if before != -1:
          return s[before+len(bpat):end] 
       return ""
    return ""
 
 
if __name__ == "__main__":
   s = '<a href="the quick brown fox">test1</a><a href="jumps over">test2</a>'
   print 'input string:\n',s
   print 'before(x, "<a")=',  before(s, "<a")
   print 'rbefore(x, "<a")=',  rbefore(s, "<a")
   print 'after(x, "<a")=',  after(s, "<a")
   print 'rafter(x, "<a")=',  rafter(s, "<a")
 
   print 'before(s, "</a>")=',before(s, "</a>")
   print 'after (s, "\">\")=',  after(s, '">')
   print """rafter (s,  '">\')=""", rafter(s, '">')
   print 'between( s, ">","</a>")=',  between(s,">","</a>")
   print 'rbetween(s, ">","</a>")=', rbetween(s,">","</a>")

Here is a sample run:

$ python strlib.py input string: test1test2 before(x, "test2 after(x, "test1test2 rafter(x, "test2 before(s, "")= test1 after (s, "">")= test1test2 rafter (s, '">')= test2 between( s, ">","")= test1 rbetween(s, ">","")= test2 $
  • Share/Bookmark

One Response to “Python:Extra string functions: before, after, between”

  1. oggie Says:

    geek na geek ka palaka talagaga!!!

Leave a Reply

Digital explorations is Digg proof thanks to caching by WP Super Cache