-2

I have a string:

str = "alskdfj asldfj 1234_important_what_i_need_123 sdlfja faslkdjfsdkf 234234_important_what_i_need_12312 alsdfj asdfj"

I want to extract each occurrence of the "%important_what_i_need%" bit from the string, including 10 or so characters before and after the search term.

How do I do this with python? Do I need to import re?

Peter Varo
  • 11,726
  • 7
  • 55
  • 77
LNA
  • 1,427
  • 3
  • 24
  • 37

1 Answers1

1

Starting with "aaafoobbb" and looking for "foo" and the surrounding two characters on either side, you could do:

>>> start_string = "aaafoobbb"
>>> search_string = "foo"
>>> index = start_string.index(search_string)
>>> s[(index - 2) : (index + len(search_string) + 2)]

Should be easy enough to adapt to your needs, although you'll need to add some extra checks to make sure your slice indices are within range (e.g. make sure that index - 2 is not less than 0). You definitely want to become more familiar with slicing and strings in Python.

Community
  • 1
  • 1
Nacho
  • 451
  • 2
  • 9
  • Thanks! Yeah, I found that the number I substituted for "2" had to not be very large/exceed the total length of start_string. Question: any particular reason you used .index() instead of .find()? .index() raises a ValueError when the substring isn't found... – LNA May 16 '14 at 01:48
  • It's just what came to mind at the time. Find is fine too, I think. Also, thanks for fixing the variable name. Clearly had not had my coffee yet. – Nacho May 16 '14 at 15:54