Can anyone help me with translating this regex to Python re module?
sed -e "s/^[^ ]* \([^ ]*\) \([0-9]*\) \([0-9:]*\) \([0-9]*\) /\1 \2 \4 \3 /"
As I’ve understood this replaces one text to another. How can I create something like this using re module? Thanks!
e.g. Fri Mar 21 07:16:51 2008 -0600 will be converted to Mar 21 2008 07:16:51 -0600.
source = 'Fri Mar 21 07:16:51 2008 -0600'
pattern = re.compile('^[^ ]* \([^ ]*\) \([0-9]*\) \([0-9:]*\) \([0-9]*\) ')
result = re.sub('\\1 \\2 \\4 \\3 ', source)
Drop the backslashes on the parens in your pattern too, as in:
The way you had it, you were escaping the parens which need to be recognized as groupings so that you actually capture the four groups corresponding to the \1 … \4