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.

2
  • 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?\] Commented Jul 29, 2014 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? Commented Jul 29, 2014 at 13:10

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

3 Comments

+1 - Also important to note that order in the addition here matters , so do 1 + not ...+ 1. The latter will concatenate a string.
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.
I would recommend asking a new question since checking if a file exists is different from incrementing a number in a file name.
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.

Comments

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

2 Comments

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.
Dito to what @Entbark said, ask another question as the 2 are quite different.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.