0

I need a python regular expression through which I can replace certain values in the path

"path":"C:\Users\admin\Downloads\npp.8.0.Installer.exe"
"path": "C:\Program Files (x86)\Google\Common\Google Updater\Extensions\GoogleUpdaterService.exe"

to

"path":"<drive>:\Users\<uname>\Downloads\npp.8.0.Installer.exe"
"path": "<drive>:\Program Files (x86)\Google\Common\Google Updater\Extensions\GoogleUpdaterService.exe"

The replace values are "<'drive'>" and "<'username'>"(if any username exists in the path).

I am trying with

re.sub(pattern, repl, string, count=0, flags=0). 

How can I most easily build a pattern which can replace the above valve ?

no1
  • 128
  • 11
  • 1
    What is your question about your need? – mkrieger1 Jun 14 '21 at 19:34
  • Why not simply use `str.replace()`? – AnsFourtyTwo Jun 14 '21 at 19:34
  • 1
    How is a regular expression supposed to know that `admin` is a user name but `Google Updater` is not? – mkrieger1 Jun 14 '21 at 19:36
  • 1
    Can you demonstrate *any* effort at solving this yourself? – Scott Hunter Jun 14 '21 at 19:36
  • 2
    "How can I most easily build a pattern" --> by [Learning Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions) – mkrieger1 Jun 14 '21 at 19:38
  • 1
    @mkrieger1 The assumption is any word right next to literal word 'User\' and before next occurrence of '\' can be considered as username. – no1 Jun 14 '21 at 19:40
  • This works for replacing users with ``: https://regex101.com/r/Ti2Eqr/1/ This works for replacing drives with ``: https://regex101.com/r/uclhE5/1 – LuisAFK Jun 14 '21 at 19:50
  • 1
    @LuisAFK Thanks for the expressions. The expression for drive isn't working when the path is in between quotes (" "). https://regex101.com/r/NpJ6Vx/1. – no1 Jun 14 '21 at 19:57
  • @no1 fixed, accepts with and without: https://regex101.com/r/NpJ6Vx/2 – LuisAFK Jun 14 '21 at 20:36
  • 1
    @LuisAFK It isn't working for the string "path":"C:\Users\admin\Downloads\npp.8.0.Installer.exe" to "path":":\Users\admin\Downloads\npp.8.0.Installer.exe" – no1 Jun 14 '21 at 20:42
  • @no1 fixed: https://regex101.com/r/NpJ6Vx/3 – LuisAFK Jun 15 '21 at 15:09

0 Answers0