1

I am having a lot of text similar t the following in my code which I am trying to edit with notepad++:

except ValueError, err:
except Exception,err:
except RPCFault, err:

Not what I am trying to do is to modify the text like the following:

except ValueError as err:
except Exception as err:
except RPCFault as err:

For making the above changes I tried to use the regex: except.*, in the find box and in replace box: except.* as

But this did not help. Kindly suggest me what I need to do.

0

In Search Mode Regular expression Replace

(except\ [A-Za-z]+),\ *(err:)

with

$1 as $2
1

Assuming there is only one comma in each line:

  • Ctrl+H
  • Find what: ^except [^,]+\K, *(?=err:$), *(?=err:$)
  • Replace with: as
  • check Wrap around
  • check Regular expression
  • Replace all

Explanation:

^                   : begining of line
  except [^,]+      : literally "except", followed by a space and 1 or more non comma
  \K                : forget all we have seen until this position
  ,                 : a comma
   *                : 0 or more spaces
  (?=               : look ahead, zero length assertion that makes sure we have after
    err:$           : literally "err:" at end of line
  )                 : end lookahead

Replacement:

 as     : a space, "as", a space

Result for given example:

except ValueError as err:
except Exception as err:
except RPCFault as err:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.