title: Python regex to convert particular pattern inside a file
body: <p>Hi I"m looking to change below patterns inside a file:</p>
  
  <pre><code>input [15:0] A;
  output [15:0] B;
  inout     [4:0]   C;
  input [3:0]     D;
  inout [0:32] X;
  output [5:2] Y;
  </code></pre>
  
  <p>to:</p>
  
  <pre><code>input A [15:0];
  output B [15:0];
  inout C [4:0];
  input D [3:0];
  inout X [0:32];
  output Y [5:2];
  </code></pre>
  
  <p>What is the best way to achieve this using python regular expressions?</p>
  
  <p>Here is what I wrote so far, it does the job but I think I could make it more efficient.
  1.It does not convert multiple white spaces into 1. 
  2.My regex only looks for the pattern  .*[po]ut. what is a good regex which can catch input/output/inout at the beginning of any line?
  3. How can modify it to edit all files inside a given directory path? </p>
  
  <pre><code>#!/usr/bin/python
  import re
  
  for line in fileinput.input(inplace=1):
      line = re.sub(r'^(.*[po]ut)(\s+\[\d+:\d+])(\s+[a-zA-Z0-9_]*);',r'\1\3\2;',line.rstrip())
      print(line)
  </code></pre>