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
CodePudding user response:
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
CodePudding user response:
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
CodePudding user response:
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|