Hi Guys,

Im having trouble with my prog..

basically i have a file with lots of details in it.

I want to find every time the file says for example "blah"

Than i want to find the next occurence of "notblah"

and i want to use the information between these two strings.

Any ideas on where i can do something like this??

Thanks in advance!

Recommended Answers

All 2 Replies

Use string.find

##   This code assumes that "blah is always in the string before "notblah"
##   and that the sequence is "blah" then "notblah" and not some variation 
##   of "blah", "blah", "notblah"
some_string="ab-blah-cde-notblah-fghij-blah-klm-notblah-no-blah-stu-notblah-xyz"
result=0
while result > -1:      ## -1 = not found
   result=some_string.find("blah", result)
   result2=some_string.find("notblah", result+1)
   if (result > -1) and (result2 > -1):
      print "blah =", result, "and notblah =", result2
      result =result2 + len("notblah")

Hmm, I needed something like that myself, so I fiddled with it:

# find the substring between two given marker strings

s = "ab blah cde notblah fghij blah klm notblah no blah stu notblah xyz"

# marker strings
s1 = "blah"
s2 = "notblah"

word_list = s.split()
start = 0
for word in word_list:
    if word == s1:
        # index of marker strings
        start = word_list.index(s1, start)
        end = word_list.index(s2, start)
        # move it up by 1
        start += 1
        # slice list
        sub_list = word_list[start: end]
        # join to form string
        sub_string = ''.join(sub_list)
        print sub_string
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.