-2

I'm trying to regex replace some groups of charactes in powershell using the following pattern:

/^(thedir[ ]=[ ]")(.)(:[/\])([a-zA-Z0-9])([/\]?.)$/gm

by using this instruction:

$fileContent = Get-Content $fileToProcess -Raw 
$fileContent -replace '(?m)^(thedir[ ]*=[ ]*")(.)(:[\/\\])([a-zA-Z0-9]*)([\/\\]?.*)$', '$1Z$3Here$5'
Write-Host "New file content is $fileContent"

but it fails doing anything at execution : $fileContent remains unchanged. I note that the /gm flags are important, and are missing in my instruction, but I don't find how to set them. I've tried to prefix the regex with (?m) without success (and also tried (?smi), why not :)):

$fileContent -replace '(?m)^(thedir[ ]*=[ ]*")(.)(:[\/\\])([a-zA-Z0-9]*)([\/\\]?.*)$', '$1Z$3Here$5'

As an example, let's process a my.ini containing:

[mysqld]
#skip-innodb

# The TCP/IP Port the MySQL Server will listen on
port=3306
max_allowed_packet=16M


#Path to installation directory. All paths are usually resolved relative to this.
#basedir="C:/Program Files/MySQL/MySQL Server 5.1/"
basedir="C:/MySQL/"

#Path to the database root
#datadir="C:/Documents and Settings/All Users/Application Data/MySQL/MySQL Server 5.1/Data/"
datadir="C:/MySQLData/"

# The default character set that will be used when a new schema or table is
# created and no character set is defined
default-character-set=utf8

and replacing thedir in the matching string by basedir

For expected result see here

As for now, I don't have any change in the $fileContent using the previsouly mentionned instructions.

Can you help me to make this work?

27
  • Instead of using a Regex, why not use .Net's file management classes? Those are aware of different path separators, drive letters and as such will return well-formed data.
    – vonPryz
    Commented Feb 20, 2024 at 9:03
  • 1
    Without giving an example of what fileContent you expect to be replaced from/to, this makes your question very difficult to understand, and hard to know if a proposed solution actually works.
    – Tom Lord
    Commented Feb 20, 2024 at 9:45
  • 1
    @Oliver My issue is that it's difficult to understand what you're trying to do. You have not provided an example of what fileContent might contain, i.e. what strings you're trying to match with the pattern. It is not easy to understand what /^(thedir[ ]=[ ]")(.)(:[/\])([a-zA-Z0-9])([/\]?.)$/gm means, let alone what's apparently "wrong" with it.
    – Tom Lord
    Commented Feb 20, 2024 at 10:17
  • 1
    Please edit your question. Add a real example of the file you are reading and below that what the expected result should be and what the current (wrong) result is that you are getting.
    – Theo
    Commented Feb 20, 2024 at 10:23
  • 1
    Heads up: -replace doesn't mutate the string in-place - you need to assign the output back to the variable if you want it to persist: $fileContent = $fileContent -replace ... Commented Feb 20, 2024 at 10:52

1 Answer 1

2

As mentioned in the comments, the pattern works perfectly fine against the sample values in the linked regex101 test suite:

enter image description here

Beware that -replace doesn't have side-effects - it doesn't modify the left-hand side variable in-place (like perl's =~ for example).

To store the resulting string(s), remember to re-assign them to a new or existing variable:

$newFileContents = $fileContents -replace '(?m)^(thedir[ ]*=[ ]*")(.)(:[\/\\])([a-zA-Z0-9]*)([\/\\]?.*)$', '$1Z$3Here$5'
Write-Host "New file contents is:`n$($newFileContents -join "`n")"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.