0

I'm not such a regex expert and frankly I'm trying to avoid it whenever I can.

I would like to create a new $String where the number within the string is updated with +1. This number can be one or two digits and will always be between 2 brackets.

From:

$String = "\\Server\c$\share_1\share2_\Taget2[1] - 2014-07-29.log"

To:

$String = "\\Server\c$\share_1\share2_\Taget2[2] - 2014-07-29.log"

Thank you for your help.

DarkLite1
  • 13,637
  • 40
  • 117
  • 214
  • 1
    Escape brackets in regex (they're meaningful, not literal): `\[\d+\]`. If you're sure number can have only one or two digits then you may also make it explicit: `\[\d[\d]\]` or `\[\d\d?\]` – Adriano Repetti Jul 29 '14 at 13:06
  • Thank you Adriano :) This seems to work partially already `$String -replace "\[\d+\]",'[bla]'` How can I update the number now within the brackets? – DarkLite1 Jul 29 '14 at 13:10

3 Answers3

4

If you want to avoid the regex:

$String = "\\Server\c$\share_1\share2_\Taget2[1] - 2014-07-29.log"
$parts = $string.Split('[]')
$Newstring = '{0}[{1}]{2}' -f $parts[0],(1 + $parts[1]),$parts[2]
$Newstring
\\Server\c$\share_1\share2_\Taget2[2] - 2014-07-29.log
mjolinor
  • 66,130
  • 7
  • 114
  • 135
  • +1 - Also important to note that order in the addition here matters , so do `1 +` not `...+ 1`. The latter will concatenate a string. – JNK Jul 29 '14 at 13:40
  • Thanks mjoninor, that is indeed a good way of doing and I was indeed wondering why the `1+` was before the string. I've updated the question to better reflect my problem. I'm sorry for the confusion. – DarkLite1 Jul 29 '14 at 14:03
  • 1
    I would recommend asking a new question since checking if a file exists is different from incrementing a number in a file name. – Benjamin Hubbard Jul 29 '14 at 14:21
1

Another option is using the Replace() method of the Regex class with a scriptblock (code taken from this answer by Roman Kuzmin):

$callback = {
  $v = [int]$args[0].Groups[1].Value
  $args[0] -replace $v,++$v
}

$filename = "\\Server\c$\share_1\share2_\Taget2[1] - 2014-07-29.log"

$re = [Regex]"\[(\d+)\]"
$re.Replace($filename, $callback)

Existing files could be handled like this:

...
$re = [Regex]"\[(\d+)\]"
while (Test-Path -LiteralPath $filename) {
  $filename = $re.Replace($filename, $callback)
}

Note that you must use Test-Path with the parameter -LiteralPath here, because your filename contains square brackets, which would otherwise be interpreted as wildcard characters.

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

With regex:

$s = "\\Server\c$\share_1\share2_\Taget2[1] - 2014-07-29.log"
$s -replace "(?<=\[)(\d+)","bla"

Result:

\\Server\c$\share_1\share2_\Taget2[bla] - 2014-07-29.log

So you can do something like this:

$s = "\\Server\c$\share_1\share2_\Taget2[1] - 2014-07-29.log"
$s -match "(?<=\[)(\d+)" | Out-Null  ## find regex matches
$newNumber = [int]$matches[0] + 1
$s -replace "(?<=\[)(\d+)",$newNumber

Result:

\\Server\c$\share_1\share2_\Taget2[2] - 2014-07-29.log
Raf
  • 9,681
  • 1
  • 29
  • 41
  • Thank you Raf, this would work. But how can you check afterwards, on a second run of the script, if that file name is already in use? It's difficult to feed the right `$Path` to `Test-Path` so it creates the new file name. – DarkLite1 Jul 29 '14 at 14:13
  • Dito to what @Entbark said, ask another question as the 2 are quite different. – Raf Jul 29 '14 at 14:25