none
replace a string using regular expression from powershell RRS feed

  • Question

  • I want to replace the following:

    'browserName': 'firefox'

    with :

    'browserName': 'chrome'

    then I tried this:

    (get-content $conffile) -replace "^('browserName': ')\S+","browserName': 'chrome' |set-content $conffile

    But nothing happened.

    Could someboby tell me how to write the regular expression here? Thanks a lot.

    Friday, April 10, 2015 7:23 PM

Answers

  • Second person today with the same question.

    get-content $conffile |%{$_ -replace "'browserName':\s+'firefox'","'browserName': 'chrome'"  | set-content $conffile


    \_(ツ)_/

    • Proposed as answer by jrv Friday, April 10, 2015 7:31 PM
    • Marked as answer by Boe ProxMVP, Moderator Sunday, July 26, 2015 3:31 AM
    Friday, April 10, 2015 7:31 PM

All replies

  • Second person today with the same question.

    get-content $conffile |%{$_ -replace "'browserName':\s+'firefox'","'browserName': 'chrome'"  | set-content $conffile


    \_(ツ)_/

    • Proposed as answer by jrv Friday, April 10, 2015 7:31 PM
    • Marked as answer by Boe ProxMVP, Moderator Sunday, July 26, 2015 3:31 AM
    Friday, April 10, 2015 7:31 PM
  • Thanks a lot. it works.

    (get-content $conffile) |% {$_ -replace "'browserName':\S+" ,"'browserName':'chrome'" }| set-content $conffile

    Friday, April 10, 2015 7:45 PM
  • You almost had it but didn't see that it was really a simple replace.

    Glad it worked for you.


    \_(ツ)_/

    Friday, April 10, 2015 7:51 PM