longbit ok i am playing around with python lately and am trying to write a code sample that take a text as an input and extract from it the 7 characters that proceed a certain string, for example: if text = aaaabbbb1234567XXXX and the string to check is XXXX then the result will be "1234567" with java i would simply do: System.out.println(text.substring(text.indexOf("XXXX")-7,text.indexOf("XXXX"))); how can i do it in python with regex or without? plus any one have a good tut for regex it will be very useful. Thank You.
Joe The exact same thing as java: text = "aaaabbbb1234567XXXX" i = text.index("XXXX") print text [i-7:i] If you want to look into regular expressions, all you could ever need is in the official Python documentation. On top of that, the official documentation contains a great Regular Expression HOWTO. These two links should contain all you need to know about python re module.
xterm At least a dozen ways to do what you want, of which half are ways to do it through regex. One of them is as follows. text = "aaaabbbb1234567XXXX" token = "XXXX" result = re.search('(\d+)%s' % token, text) if result: result.group(1)