title: token capture sequences python not working correctly
body: <p>I am testing out my python grepper script.  It takes multiple search patterns, and works well with regular expressions and plain text.</p>
  
  <p>For this test, my input.txt has the following two lines:</p>
  
  <pre><code>foo blah is a bar
  foo blah is bar
  </code></pre>
  
  <p>If I run: </p>
  
  <pre><code>cat input.txt | ./pgreper.py "foo %{0} is a %{1}"
  </code></pre>
  
  <p>I should get: foo blah is a bar, as output, however I get nothing.</p>
  
  <p>Any help in this area would be greatly appreciated.</p>
  
  <p>Thanks :)</p>
  
  <p>ps please ignore my comments.</p>
  
  <pre><code>#!/usr/bin/python3
  
  import sys
  import re
  import time
  import datetime
  import inspect
  import argparse
  
  # The following section allows arguments to be passed to the module, eg input | ./pgreper.py pattern pattern --debug
  parser = argparse.ArgumentParser(description='Python Grep.')
  # Enable debugging, by adding --debug at the end of command
  parser.add_argument('--debug', action='store_true', help='Print debug messages')
  # nargs='+', enables multiple patterns to be entered at the commandline eg input | ./pgreper.py pattern pattern
  parser.add_argument('pattern', type=str, nargs='+', help='Pattern(s) for pgrepping')
  args = parser.parse_args()
  
  
  # This is the class that allows for debugging a line in sys.stdin, if it matches all patterns.
  class CodeTrace(object):
      def __init__(self, line, pattern):
          self.line = line
          self.pattern = pattern
  
      # @staticmethod
      # This is the degugging method
      def trace(self, line, pattern):
          # Capture the current time, and format the timestamp, into a readable format
          ts = datetime.datetime.fromtimestamp(time.time()).strftime('[%Y-%m-%d %H:%M:%S:%f]')
          # Inspect allows us to blah, inspecting the stack allows us to retrieve information
          stack = inspect.stack()
          # Retrieve calling class information
          the_class = stack[1][0].f_locals["self"].__class__
          # Retrieves the calling method information
          the_method = stack[1][0].f_code.co_name
          # Retrieves the calling method's variables
          the_variables = stack[1][0].f_code.co_varnames
          # Formats the contents of the debug trace into a readable format,
          # Any parameters passed to the method and the return value, are included in the debug trace
          debug_trace = ("{} {}.{}.{} {} {} ".format(ts, str(the_class), the_method,the_variables, pattern, line))
          # Send out the debug trace as a standard error output
          sys.stderr.write(debug_trace + "\n")
  
  
  # This is the class that does the pattern matching
  class Grepper(object):
      def __init__(self, patterns, debug=False):
          # Every pattern that this module takes is compiled here, so that it may be searched for in sys.stdin
          self.patterns = [re.compile(p) for p in patterns]
          self.debug = debug
  
      # This method compares the input, to the patterns compiled in Grepper
      def matchline(self, debug):
          for line in sys.stdin:
              # This line compares all the patterns to the input, only if the input matches ALL patterns does it pass
              if all(p.search(line) for p in self.patterns):
                  sys.stdout.write(line)
                  # this if statement runs the CodeTrace.trace function, if the user adds the --debug option in the cli
                  if self.debug:
                      CodeTrace(line, self.patterns).trace(line, args.pattern)
  
  
  # This main function calls the grepper class and the matchline method, for the purpose of pattern matching
  def main():
      print(args.pattern)
      Grepper(args.pattern, args.debug).matchline(args.debug)
  
  # This allows the module to be used as a standalone module, or a reusable module in a different program
  if __name__ == "__main__":
      main()
  </code></pre>