Extract unspecified length of characters to the right of a pattern - Powershell - powershell

I have a function that returns a string value:
"D:\Put_Your_Temporary_Files_HERE\Auto_Receive\user_out-automation.ini:9:IpAccessListEx=1|131.203.181.66|1|10.21.5.34|1|109.146.13.135|1|10.21.3.3|"
Everything after the equals sign can change depending on what's return in my function. What I want to do is return all text after the equals (=) sign.
Any help will be greatly appreciated.
Thanks
Rob

You can use Select-String Cmdlet with -Pattern parameter to specify the text to find on input
$r = "IPAddressEx=|2.33.31.45|108.38.48.17|" | Select-String -Pattern "IPAddressEx=(.*)"
It return Microsoft.PowerShell.Commands.MatchInfo object on successful pattern match. So to get the sub group from match you have to the access Matches property.
$r.Matches.Groups[1].Value

You can do a simple -split like
$str = "D:\Put_Your_Temporary_Files_HERE\Auto_Receive\user_out-automation.ini:9:IpAccessListEx=1|131.203.181.66|1|10.21.5.34|1|109.146.13.135|1|10.21.3.3|"
($str -split '=', 2)[-1]
Result: 1|131.203.181.66|1|10.21.5.34|1|109.146.13.135|1|10.21.3.3|
OR use -replace:
$str = "D:\Put_Your_Temporary_Files_HERE\Auto_Receive\user_out-automation.ini:9:IpAccessListEx=1|131.203.181.66|1|10.21.5.34|1|109.146.13.135|1|10.21.3.3|"
$str -replace '.*=(.+)$', '$1'
Result: 1|131.203.181.66|1|10.21.5.34|1|109.146.13.135|1|10.21.3.3|
OR use the regex .Match() method
$str = "D:\Put_Your_Temporary_Files_HERE\Auto_Receive\user_out-automation.ini:9:IpAccessListEx=1|131.203.181.66|1|10.21.5.34|1|109.146.13.135|1|10.21.3.3|"
([regex]'.*=(.+)$').Match($str).Groups[1].Value
Result: 1|131.203.181.66|1|10.21.5.34|1|109.146.13.135|1|10.21.3.3|
OR even the String methods IndexOf() combined with SubString():
$str = "D:\Put_Your_Temporary_Files_HERE\Auto_Receive\user_out-automation.ini:9:IpAccessListEx=1|131.203.181.66|1|10.21.5.34|1|109.146.13.135|1|10.21.3.3|"
$str.Substring($str.IndexOf("=") + 1)
Although this last alternative is not advisable because IndexOf() may return -1 if the search string is not found

An alternative to Abdul's answer is to use regular expressions. For your case, this should work:
$ret = [regex]::match($string,'(?<=.*=).*')
if ($ret.Success) {
$tag = $ret.Value
}
The regex uses a positive lookbehind, which finds all characters which precede an '='. It then finds all characters which follow it. It stores the object in $ret. You can get your value with $ret.Value.
This returns 1|131.203.181.66|1|10.21.5.34|1|109.146.13.135|1|10.21.3.3|

Related

powershell How to capture 'left side' of the -replace regex?

First time posting.
i have the following code to replace the suffix of an email and its working fine
replace all characters after # sign with #testdomain.com
$a = 'john.doe#domain.com'
$b = $a -replace "[?=#].*", '#testdomain.com'
$b
john.doe#testdomain.com
what i would like to do, is to capture the actual left side 'source' regex expression to a variable, which would be #domain.com so that i know what i;m replacing and i don;t know how to do it.
Sorry if this had been posted before.
Thank you
So, I'm not sure if this is possible using only the -replace operator and without the use of -match which would store the capture group on the $Matches automatic variable.
This is how you could do it using the regex class directly:
$a = 'john.doe#domain.com'
$Capture = #{}
$b = [regex]::Replace($a, "[?=#].*", {
param($s)
'#testdomain.com'
$Capture.Value = $s.Value
})
$b # => john.doe#testdomain.com
$Capture.Value # => #domain.com
This what I could think using only -replace, adding a delimiter (// in this case) to the replaced string followed by the capture group $0 and then splitting the replacement. Though, this is obviously not a robust solution.
$a = 'john.doe#domain.com'
$b, $capture = $a -replace "[?=#].*", '#testdomain.com//$0' -split '//'
$b # => john.doe#testdomain.com
$capture # => #domain.com
To change the user part you can replace ^.*(?=#):
PS ~> $a = 'john.doe#domain.com'
PS ~> $a -replace '^.*(?=#)', 'jane.doe'
jane.doe#domain.com
The (?=#) construct is known as a lookahead, and describes a zero-length string at the position immediately before the #.

PowersShell UpperCase Replace

Ok... after 99 different combinations...
I want to replace in thousands of files all occurences of EnumMessage.something to EnumMessage.SOMETHING so uppercase the second word. Which may be standalone or followed by a dot or followed by a (
$output = 'EnumMessage.test(something) and EnumMessage.Tezt andz EnumMessage.ALREAdY. ' -creplace 'EnumMessage\.(\w+)', 'EnumMessage.$1.ToUpper()'
$output
So the above places the function Upper there (the word) but it does not upper the second word.
In PowerShell 6 and later, the -replace operator also accepts a script block that performs the replacement. The script block runs once for every match.
In PowerShell 5, apply the Regex.Replace Method.
$string = 'EnumMessage.test(something) and EnumMessage.Tezt andz EnumMessage.ALREAdY. '
$pattern = '(?<=EnumMessage\.)(\w+)'
# (?<=EnumMessage\.) = positive lookbehind
if ( $PSVersionTable.PSVersion.Major -ge 6 ) {
$string -replace $pattern, { $_.Value.ToUpper() }
} else {
[regex]::Replace( $string, $pattern, { $args.Value.ToUpper() } )
}
There are definitely some challenges. I'm really not the best at RegEx. Any time I tried to leverage the $matches collection I was only able to replace the first match. There's probably something I'm forgetting about that functionality. However, I was able to cook up the below:
[RegEx]::Matches($String, '(?<=EnumMessage\.)\w+') |
ForEach-Object{
$Replace = $String.Substring($_.Index, $_.Length).ToUpper()
$String = $String.Remove($_.Index, $_.Length)
$String = $String.Insert($_.Index, $Replace)
}
$String
Note: I used a RegEx lookbehind, but I'm not positive that had much to do with the outcome.
The .Net [RegEx] class returned objects with the location of the matches in the string so I used that to strategically remove then add the ucased strings. Which should return: EnumMessage.TEST(something) and EnumMessage.TEZT andz EnumMessage.ALREADY.

Is there a way to replace the first two occurrences of a value in a string?

I am going to be as clear with my question as I can.
I might be missing something very obvious here but I just don't know how to find a solution...
I have a string and I would like to replace the first two occurrences of ":" with "/":
String:
$string = 2020:10:07 08:45:49
Desired String:
2020/10/07 08:45:49
I have tried using .Replace as seen below:
$string = $string.Replace([regex]":","/",2)
But I am given this error every time:
Cannot find an overload for "replace" and the argument count: "3".
I have seen others use .Replace in this way before so I'm not sure what is so different about my usage. Can anyone point me in the right direction?
PowerShell is .net-based language.
String does not have overload method Replace with anything like count argument in .Net, but Python's string does.
You can use this:
$string = '2020:10:07 08:45:49'
#Replace 2+ spaces you have in source with single space
$string = $string -replace '\s+', ' '
# Variant 0 - Best - ALWAYS use proper types. There is DateTime type to use for Dates and Times!
#Exact parse by format to DateTime object
$dt = [DateTime]::ParseExact($string, 'yyyy:MM:dd hh:mm:ss', [System.Globalization.CultureInfo]::InvariantCulture)
#Convert DateTime to String
$result = $dt.ToString('yyyy\/MM\/dd hh:mm:ss')
.Net's String.Split has optional parameter count that means split no more than into # pieces. You can use it:
# Variant1
$result = [string]::Join('/',$string.Split(':', 3))
# Variant2
$result = $string.Split(':', 3) -join '/'
String.Replace() does not support regex patterns, nor does it accept a maximum count.
Use the -replace regex operator instead:
$string = '2020:10:07 08:45:49'
$string -replace '(?<=^[^:]*):(.*?):','/$1/'
This will replace only the first and second occurrence of : with /
Specifically for date/time representations, you may want to parse it as such, at which point you can easily re-format it:
$string = '2020:10:07 08:45:49'
$datetime = [datetime]::ParseExact($string, "yyyy:MM:dd HH:mm:ss", $null)
# Now we can create a new string with the desired format
Get-Date $datetime -Format 'yyyy/MM/dd HH:mm:ss'
# This might be easier than figuring out regex patterns
'{0:dd/MMM/yyyy-HH.mm.ss}' -f $datetime

Extract string between two special characters in powershell

I need to extract a list with strings that are between two special characters (= and ;).
Below is an example of the file with line types and the needed strings in bold.
File is a quite big one, type is xml.
<type="string">data source=**HOL4624**;integrated sec>
<type="string">data source=**HOL4625**;integrated sec>
I managed to find the lines matching “data source=”, but how to get the name after?
Used code is below.
Get-content regsrvr.txt | select-string -pattern "data source="
Thank you very much!
<RegisteredServers:ConnectionStringWithEncryptedPassword type="string">data source=HOL4624;integrated security=True;pooling=False;multipleactiveresultsets=False;connect timeout=30;encrypt=False;trustservercertificate=False;packet size=4096</RegisteredServers:ConnectionStringWithEncryptedPassword>
<RegisteredServers:ConnectionStringWithEncryptedPassword type="string">data source=HOL4625;integrated security=True;pooling=False;multipleactiveresultsets=False;connect timeout=30;encrypt=False;trustservercertificate=False;packet size=4096</RegisteredServers:ConnectionStringWithEncryptedPassword>
The XML is not valid, so it's not a clean parse, anyway you can use string split with regex match:
$html = #"
<RegisteredServers:ConnectionStringWithEncryptedPassword type="string">data source=HOL4624;integrated security=True;pooling=False;multipleactiveresultsets=False;connect timeout=30;encrypt=False;trustservercertificate=False;packet size=4096</RegisteredServers:ConnectionStringWithEncryptedPassword>
<RegisteredServers:ConnectionStringWithEncryptedPassword type="string">data source=HOL4625;integrated security=True;pooling=False;multipleactiveresultsets=False;connect timeout=30;encrypt=False;trustservercertificate=False;packet size=4096</RegisteredServers:ConnectionStringWithEncryptedPassword>
"#
$html -split '\n' | % {$null = $_ -match 'data source=.*?;';$Matches[0]} |
% {($_ -split '=')[1] -replace ';'}
HOL4624
HOL4625
Since the connectionstring is for SQL Server, let's use .Net's SqlConnectionStringBuilder to do all the work for us. Like so,
# Test data, XML extraction is left as an exercise
$str = 'data source=HOL4624;integrated security=True;pooling=False;multipleactiveresultsets=False;connect timeout=30;encrypt=False;trustservercertificate=False;packet size=4096'
$builder = new-object System.Data.SqlClient.SqlConnectionStringBuilder($str)
# Check some parameters
$builder.DataSource
HOL4624
$builder.IntegratedSecurity
True
You can expand your try at using Select-String with a better use of regex. Also, you don't need to use Get-Content first. Instead you can use the -Path parameter of Select-String.
The following Code will read the given file and return the value between the = and ;:
(Select-String -Path "regsrvr.txt" -pattern "(?:data source=)(.*?)(?:;)").Matches | % {$_.groups[1].Value}
Pattern Explanation (RegEx):
You can use -pattern to capture an String given a matching RegEx. The Regex can be describe as such:
(?: opens an non-capturing Group
data source= matches the charactes data source=
) closes the non-capturing Group
(.*?) matches any amount of characters and saves them in a Group. The ? is the lazy operator. This will stop the matching part at the first occurence of the following group (in this case the ;).
(?:;) is the final non-capturing Group for the closing ;
Structuring the Output
Select-String returns a Microsoft.PowerShell.Commands.MatchInfo-Object.
You can find the matched Strings (the whole String and all captured groups) in there. We can also loop through this Output and return the Value of the captured Groups: | % {$_.groups[1].Value}
% is just an Alias for For-Each.
For more Informations look at the Select-String-Documentation and try your luck with some RegEx.

Question regarding incrementing a string value in a text file using Powershell

Just beginning with Powershell. I have a text file that contains the string "CloseYear/2019" and looking for a way to increment the "2019" to "2020". Any advice would be appreciated. Thank you.
If the question is how to update text within a file, you can do the following, which will replace specified text with more specified text. The file (t.txt) is read with Get-Content, the targeted text is updated with the String class Replace method, and the file is rewritten using Set-Content.
(Get-Content t.txt).Replace('CloseYear/2019','CloseYear/2020') | Set-Content t.txt
Additional Considerations:
General incrementing would require a object type that supports incrementing. You can isolate the numeric data using -split, increment it, and create a new, joined string. This solution assumes working with 32-bit integers but can be updated to other numeric types.
$str = 'CloseYear/2019'
-join ($str -split "(\d+)" | Foreach-Object {
if ($_ -as [int]) {
[int]$_ + 1
}
else {
$_
}
})
Putting it all together, the following would result in incrementing all complete numbers (123 as opposed to 1 and 2 and 3 individually) in a text file. Again, this can be tailored to target more specific numbers.
$contents = Get-Content t.txt -Raw # Raw to prevent an array output
-join ($contents -split "(\d+)" | Foreach-Object {
if ($_ -as [int]) {
[int]$_ + 1
}
else {
$_
}
}) | Set-Content t.txt
Explanation:
-split uses regex matching to split on the matched result resulting in an array. By default, -split removes the matched text. Creating a capture group using (), ensures the matched text displays as is and is not removed. \d+ is a regex mechanism matching a digit (\d) one or more (+) successive times.
Using the -as operator, we can test that each item in the split array can be cast to [int]. If successful, the if statement will evaluate to true, the text will be cast to [int], and the integer will be incremented by 1. If the -as operator is not successful, the pipeline object will remain as a string and just be output.
The -join operator just joins the resulting array (from the Foreach-Object) into a single string.
AdminOfThings' answer is very detailed and the correct answer.
I wanted to provide another answer for options.
Depending on what your end goal is, you might need to convert the date to a datetime object for future use.
Example:
$yearString = 'CloseYear/2019'
#convert to datetime
[datetime]$dateConvert = [datetime]::new((($yearString -split "/")[-1]),1,1)
#add year
$yearAdded = $dateConvert.AddYears(1)
#if you want to display "CloseYear" with the new date and write-host
$out = "CloseYear/{0}" -f $yearAdded.Year
Write-Host $out
This approach would allow you to use $dateConvert and $yearAdded as a datetime allowing you to accurately manipulate dates and cultures, for example.