locked
increment a 1 rather than add a one RRS feed

  • Question

  • Hello, i have this line in a script (below)that adds a one to a surname , however if the surname1 already exists it adds another 1.

    so i get surname11.

    Is there a way to increment the 1 so if surname1 exists it increments the 1 so i get surname2 rather than surname 11?

    Thanks

     $ReTest= $email.Split("@")[0] + $Num + "@" + $email.Split("@")[1]

    Friday, October 25, 2019 3:45 PM

All replies

  • $surname = 'surname1'
    if($surname -match '(.*)(\d+$)'){
    	$x = [int]$matches[2]
    	$x++
    	$matches[1] + $x
    }
    $surname = 'surname9'
    if($surname -match '(.*)(\d+$)'){
    	$x = [int]$matches[2]
    	$x++
    	$matches[1] + $x
    }



    \_(ツ)_/


    Friday, October 25, 2019 4:19 PM
  • If you only ever want to replace a last 1 with a last 2 then this is how:

    $surname -replace '1$','2'


    \_(ツ)_/

    Friday, October 25, 2019 4:23 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, November 8, 2019 1:09 PM