1

I want to search number with specific format from string and increment the last number by 1 using powershell. The string is taken from large txt file.

Example:

$str = "Android version 4.4.2, Alfa = 1.2.0400, Beta = 2.0.0200, Prod 3.4.0104"

I want to increase beta version by 1 (2.0.0200 ==> 2.0.0201),

After run powershell command:

$str = "Android version 4.4.2, Alfa = 1.2.0400, Beta = 2.0.0201, Prod 3.4.0104"

4 Answers 4

3

This is a bit clunky, but seems to work:

$str = "Android version 4.4.2, Alfa = 1.2.0400, Beta = 2.0.0201, Prod 3.4.0104"

if($str -match "(?<=Beta = \d+\.\d+\.)(?<bv>\d+)")
{
    $str -replace "(?<=Beta = \d+\.\d+\.)(\d+)", ("{0:0000}" -f (([int]::Parse($matches.bv)+1)))
}

There might be a way to get it to work in one line with just the -Replace, but it eludes me for the moment.

Sign up to request clarification or add additional context in comments.

4 Comments

I fail to understand the desire that people have to force a one line solution. I often feel it obfuscates what the code is doing for no real gain. This is not a criticism of your answer by the way. It is perfectly valid. Just an observation of a trend that I actually find to be detrimental. Especially for people that are new to a language.
@EBGreen. I totally agree. My comment wasn't worded correctly - normally, I'd not try to force a 1-liner, but in this case -replace allows you to reference matches ($1, ${name}, etc), which would be much neater without obfuscation - I just couldn't make it work :-(
Thanks for your answer, The idea isn't to write it in 1 line. I just want to know how to write this in clean way....
I feel that one liners should stay in the command line. If you are going to put your scripting logic in a file go ahead and write the verbose version for readability.
2

There is a version type in powershell you can take advantage of like so. The following will output a the beta version string incremented.

$originalString = "Android version 4.4.2, Alfa = 1.2.0400, Beta = 2.0.0200, 
Prod 3.4.0104"

$parsedString = $originalString.Split(' ')
$betaVersionStr = $parsedString[8].TrimEnd(',')
$betaVersion = [version]$betaVersionStr

# you can always condense those lines above if you want
# $betaVersion = [version]$originalString.Split(' ')[8].TrimEnd(',')

"{0}.{1}.{2:d4}" -f $betaVersion.Major, $betaVersion.Minor, ($betaVersion.Build + 1)

Comments

0

Well, the answer will depend a lot on how representative your example is of the rest of your actual data, but for the example you gave:

$str = "Android version 4.4.2, Alfa = 1.2.0400, Beta = 2.0.0200, Prod 3.4.0104"
$str -match ('^(.+), (Beta = )(.+),(.+)$')
$ver = $matches[3].split('.')
$ver[2] = '{0:0000}' -f ([int]$ver[2] + 1)
$newVerStr = $ver -join '.'
$newStr = '{0}, {1}{2},{3}' -f $matches[1], $matches[2], $newVerStr, $matches[4]
$newStr

1 Comment

There are cleaner ways to do that regex. I'll edit if I get a chance.
0

The code snippet using the method Replace of System.Text.RegularExpressions.Regex:

$str = "Android version 4.4.2, Alfa = 1.2.0400, Beta = 2.0.0200, Prod 3.4.0104"
[regex]::Replace($str, '(?<Head>.+\.)(?<Beta>\d+)(?<Tail>,[^,]+)$',
  { param( [System.Text.RegularExpressions.Match]$m )
    $bv = 1 + $m.Groups['Beta'].Value
    '{0}{1:0000}{2}' -f $m.Groups['Head'].Value,$bv,$m.Groups['Tail'].Value
  }
)

The output:

Android version 4.4.2, Alfa = 1.2.0400, Beta = 2.0.0201, Prod 3.4.0104

Comments

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.